0

I tried WebClient, HttpWebRequest, WebRequest and couple other ways to download file from specific server but every time the file is empty (0 byte). I discovered that in the response headers:

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

the Conent-Length is 0. I've opened the URL in my browser and it forced it to download file. But how can I download file in C#?

r9s
  • 227
  • 2
  • 3
  • 13
  • Check the differences in the request between fetching it in C# and fetching it with a browser. The server may be looking at the user agent, for example... – Jon Skeet Jul 08 '14 at 11:05
  • I set request headers as the same it was in my browser. – r9s Jul 08 '14 at 11:07
  • Well I suggest you use Wireshark to work out *exactly* what the differences are. – Jon Skeet Jul 08 '14 at 11:50
  • I don't know how to use Wireshark (I tried couple times). I use HttpFox addon in Firefox and Developer Tools in Chrome and IE. – r9s Jul 08 '14 at 12:49
  • Wireshark isn't too hard to use these days - I suggest you find a good tutorial. It's well worth learning about. – Jon Skeet Jul 08 '14 at 13:01

2 Answers2

0

Pass the URL to the WebMethod hope it works for you.

 [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";
            }
Sanjeev Sangral
  • 1,385
  • 2
  • 25
  • 37
  • @r9s I don't know brother either you test it or not but i know that this will help someone definitely who trying to do that. if you had completed this please add your own answer thanks. – Sanjeev Sangral Sep 12 '16 at 06:10
0
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)); 
}

------------------------------------------------------------------------
mayurgnu
  • 1
  • 2