Downloaded file using webclient.DownloadFileAsync has 0KB

前端 未结 4 1953
情话喂你
情话喂你 2020-12-11 14:43

I\'m trying to download zend-framework (from http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip) simply using WebClient

strin         


        
相关标签:
4条回答
  • 2020-12-11 14:49

    If you have UAC enabled in Windows "C:\temp.zip" in the following line will fail to save the file because you aren't allowed to write outside of user directories without elevated permissions:

    downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
    
    0 讨论(0)
  • 2020-12-11 15:02

    Just my guess: maybe you can try to keep the WebClient instance in some place would not be garbage collected. When the DownloadFileCompleted event fired, you just clean the reference to the WebClient instance and let GC to reclaim the memory later (and don't forget to call Dispose method).

    0 讨论(0)
  • 2020-12-11 15:05

    Ok, I finnaly found the answer! Before downloading the file, I was checking its size by sending HttpWebRequest. The problem was, that i didn't Close() the response.

    Thanks for the answers, they were nice clues.

    0 讨论(0)
  • 2020-12-11 15:07

    Try to handle the DownloadProgressChanged and DownloadFileCompleted event.

    private void button1_Click(object sender, EventArgs e)
      {
       string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip";
       WebClient downloader = new WebClient();
       downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(downloader_DownloadFileCompleted);
       downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
       downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
      }
    
     void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
       {
          label1.Text = e.BytesReceived + " " + e.ProgressPercentage;
        }
      void downloader_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
       {
           if (e.Error != null)
             MessageBox.Show(e.Error.Message);
           else
             MessageBox.Show("Completed!!!");
       }
    
    0 讨论(0)
提交回复
热议问题