正在下载的文件太大
file being downloaded is too large in filesize
我使用以下代码下载 excel 文件,它可以工作,但下载的文件太大。
using (var wc2 = new WebClient())
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
wc2.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
//
//wc2.DownloadFileAsync(fileUri, AppDomain.CurrentDomain.BaseDirectory + "\demo\" + fileName);
wc2.DownloadFile("https://ds.postnord.com/v2/ptm/file/download/5184.22306", AppDomain.CurrentDomain.BaseDirectory + "\demo\test.xls");
}
如果我使用浏览器下载文件,它工作正常,但不使用上面的代码。但是,如果我尝试使用上面的代码从某个地方下载一个 jpg 文件,它就可以工作。这里可能有什么问题?
如果使用 WebClient 下载的 Excel 个文件已损坏:
您应该添加 User-Agent header,接受 header 并使用接受编码。
另外,需要授权;否则,下载的文件将为 read-only.
以下代码将下载您的 excel 文件而不会损坏或出现任何其他问题:
string Url = "https://ds.postnord.com/v2/ptm/file/download/5184.22306";
string accessToken = "" ;
WebClient c = new WebClient();
c.Headers.Add("Accept: text/html, application/xhtml+xml, application/pdf, */*");
c.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
c.Headers.Add("Accept-Encoding: gzip, deflate, br");
c.Headers["Authorization"] = accessToken;
c.DownloadFile(Url, @"c:\downloaded6.xlsx");
我使用以下代码下载 excel 文件,它可以工作,但下载的文件太大。
using (var wc2 = new WebClient())
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
wc2.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
//
//wc2.DownloadFileAsync(fileUri, AppDomain.CurrentDomain.BaseDirectory + "\demo\" + fileName);
wc2.DownloadFile("https://ds.postnord.com/v2/ptm/file/download/5184.22306", AppDomain.CurrentDomain.BaseDirectory + "\demo\test.xls");
}
如果我使用浏览器下载文件,它工作正常,但不使用上面的代码。但是,如果我尝试使用上面的代码从某个地方下载一个 jpg 文件,它就可以工作。这里可能有什么问题?
如果使用 WebClient 下载的 Excel 个文件已损坏:
您应该添加 User-Agent header,接受 header 并使用接受编码。
另外,需要授权;否则,下载的文件将为 read-only.
以下代码将下载您的 excel 文件而不会损坏或出现任何其他问题:
string Url = "https://ds.postnord.com/v2/ptm/file/download/5184.22306";
string accessToken = "" ;
WebClient c = new WebClient();
c.Headers.Add("Accept: text/html, application/xhtml+xml, application/pdf, */*");
c.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
c.Headers.Add("Accept-Encoding: gzip, deflate, br");
c.Headers["Authorization"] = accessToken;
c.DownloadFile(Url, @"c:\downloaded6.xlsx");