web client DownloadFileCompleted get file name

前端 未结 2 448
借酒劲吻你
借酒劲吻你 2021-01-12 21:08

I tried to download file like this:

WebClient _downloadClient = new WebClient();

_downloadClient.DownloadFileCompleted += DownloadFileCompleted;
_downloadCl         


        
2条回答
  •  [愿得一人]
    2021-01-12 21:19

    I was playing around DownloadFileCompleted to get the file path/file name From the event. I've tried the above solution also but It was not like my expectation then i fond the solution by adding Querystring value, Here i would like to share the code with you.

    string fileIdentifier="value to remember";
    WebClient webClient = new WebClient();
    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCompleted);
    webClient.QueryString.Add("file", fileIdentifier); // here you can add values
    webClient.DownloadFileAsync(new Uri((string)dyndwnldfile.path), localFilePath);
    

    And the event can be defined as like this:

     private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
     {
         string fileIdentifier= ((System.Net.WebClient)(sender)).QueryString["file"];
         // process with fileIdentifier
     }
    

提交回复
热议问题