1

I got a WPF application and I want to download a file.

I'm using System.Net; and I have the following code:

WebClient ww = new WebClient();
ww.DownloadFileAsync(
    new Uri("http://www.sinvise.net/tester/1.jpg"), 
    AppDomain.CurrentDomain.BaseDirectory + "\\1.jpg");

The problem is, is that it doesn't download the file, it's just showing up as 0kb file and not downloading, I don't know what the problem is, can anyone help?

Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126
  • I assume www is a typo. If not, there's a problem immediately. – spender Mar 23 '10 at 01:15
  • I tried running the download on my machine and it downloaded just fine (without any additional headers or anything special). Check if you don't have some firewall or something like that blocking the transfer. Nice picture btw :-) – Tomas Petricek Mar 23 '10 at 01:50
  • EDIT: it was my av, quite silly, it didn't even prompt me! – Sandeep Bansal Mar 23 '10 at 01:58

4 Answers4

7

How about listening for the DownloadFileCompleted event and checking the AsyncCompletedEventArgs.Error property the event forwards to your handler?

    public static void DownLoadFileInBackground(string address)
    {
        WebClient client = new WebClient();
        Uri uri = new Uri(address);
        client.DownloadFileCompleted += (sender,e)=>
                                        {
                                            //inspect e here:
                                            //e.Error
                                        };
        client.DownloadProgressChanged += (sender,e)=>
                                          {
                                              //e.ProgressPercentage
                                          };
        client.DownloadFileAsync(uri, "blabla");
    }
spender
  • 117,338
  • 33
  • 229
  • 351
  • I gave it a try and used e.error to check for any output and output the progresspercentage to a progress bar and tried again with a messagebox but there was no output, neither event started. – Sandeep Bansal Mar 23 '10 at 01:20
  • I found adding the two events cleared up the intermittent issues - which is odd because this is one of the only times I needed to add events to ensure reliable writing of the file. – Dave Friedel Mar 20 '15 at 14:34
3

Some websites block requests that dont have certain headers in the request. In particular one i have found in the past is the "User-Agent" header, try copying a header from a browser request and add it into your WebClient

WebClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
Lightweight
  • 502
  • 1
  • 8
  • 19
0

Found the answer, I read that DownloadFile checks the DNS first before anything, if I use an IP address it doesn't do the check and immediately works.

Thanks for everyones help on this though.

Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126
0

I would like to add that the DownloadFileAsync method (I can't speak for DownloadFile) does not work when you have an existing and unclosed webrequest for the same file. At least that is my experience. It may be it is not allowed by the framework or server.

Kevin
  • 53,822
  • 15
  • 101
  • 132
Daniel
  • 1