开发者

Code fails to download a file more than once

Whenever I click a button in my GUI this code gets executed

this.f开发者_开发问答ile_name = @"c:\temp\file_" + DateTime.Now.Ticks / 10000 +".pdf";

client.DownloadFileCompleted +=
                    new AsyncCompletedEventHandler(pdfDownloadComplete);

client.DownloadFileAsync(new Uri(uri), file_name);

It's supposed to download a pdf file. It works fine the first time I click it, but the second time all it does is it creates an empty file in the temp directory and downloads nothing, I click the same button afterwards nothing new happens.

I cannot figure it out why it won't download more than once.

Later Edit

This is the complete code which is needed http://pastie.org/private/y7na2f4fjdu6anzteoa

I noticed that if I remove the download that checks for content type, the app downloads files without a problem

client.HeadOnly = true;
byte[] body = client.DownloadData(uri); // note should be 0-length
string type = client.ResponseHeaders["content-type"];
client.HeadOnly = false;

Still, I need to know if I'm getting text of a file from that URL so I need to make that call.


Your code has at least one issue that might be root of the problem:
You do an async download inside a using block. I don't really know what happens if the download is still running when the scope of the using block is left, but I guess that it is canceled. You should avoid that problem by using DownloadFile instead of DownloadFileAsync.
Additionally, please check whether it works, when you use the normal WebClient class and not your MyClient class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜