c# - WebClient - DownloadFileAsync not working when called second time -


i've build simple method downloads single file. when call method first time works fine, when called second time file isn't downloaded.
below code:

public void downloadfile(string fileurl, string path) {     using (var webclient = new webclient())     {         webclient.downloadfilecompleted += (sender, e) =>         {             if (e.error == null & !e.cancelled)             {                 debug.writeline(@"download completed!");             }         };          var url = new uri(fileurl);          try         {             webclient.openread(url);             string headercontentdisposition = webclient.responseheaders["content-disposition"];             string filename = new contentdisposition(headercontentdisposition).filename;              debug.writeline(filename);              path = path.combine(path, filename);             webclient.downloadfileasync(url, path);         }         catch (exception ex)         {             messagebox.show(ex.message);         }     } } 

i've identified part breaking download, part responsible getting file name:

webclient.openread(url); string headercontentdisposition = webclient.responseheaders["content-disposition"]; string filename = new contentdisposition(headercontentdisposition).filename; 

if replace part string filename = "1.tmp"; i'm able call method multiple times without errors.

i'm calling method clicking button click event:

private void button1_click(object sender, eventargs e) {     const string url = @"http://www.jtricks.com/download-text";     const string target = @"d:\temp\";     downloadfile(url, target); } 

after 2 click on button without code getting file name output in console:

1.tmp download completed! 1.tmp download completed! 

below gif showing working fine: enter image description here

when add part getting file name output:

content.txt download completed! content.txt 

below gif showing behavior: enter image description here

second time click start i'm getting file name, download don't start, next click blocks start button.

how can fix this? ideally i'd call downloadfile many time need.

it seems webclientis using cache. suggest have tell webclient not use caching:

webclient.cachepolicy = new requestcachepolicy(requestcachelevel.nocachenostore); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -