如何在c#中下载Content-Length: 0的文件

本文关键字:文件 Content-Length 下载 | 更新日期: 2023-09-27 18:12:09

我尝试了WebClient, HttpWebRequest, WebRequest和其他一些从特定服务器下载文件的方法,但每次文件都是空的(0字节)。我发现在响应头中:

Pragma: Public
Content-Disposition: attachment; filename="hQPDAU0.mp3"
Content-Transfer-Encoding: binary
Connection: close
Accept-Ranges: bytes
Content-Length: 0
Cache-Control: max-age=1468800
Content-Type: audio/mpeg
Date: Tue, 08 Jul 2014 08:52:05 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Set-Cookie: sessioncode=4v0jgqiq.....1kulouk0c01; path=/; domain=.domain
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.4.29

content - length为0。我在浏览器中打开了URL,它强制它下载文件。但是我如何在c#中下载文件?

如何在c#中下载Content-Length: 0的文件

将URL传递给WebMethod希望它能为你工作。

 [WebMethod]
            public static string ProcessIT(string downloadURL, string file_name)
            {            
                // Create a new WebClient instance.
                WebClient myWebClient = new WebClient();
                string  path = @"c:'";
                string path_n_name = path + file_name; 
                // Download the Web resource and save it into the current filesystem folder.
                myWebClient.DownloadFile(downloadURL, path_n_name);
                return "SUCCESS";
            }
public FileResult DownloadExcel(string filepath) 
{ 
byte[] fileBytes = System.IO.File.ReadAllBytes(filepath); 
   return   File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet,
             `enter code here`Path.GetFileName(filepath)); 
}
------------------------------------------------------------------------