Download file and automatically save it to folder

后端 未结 6 622
夕颜
夕颜 2020-12-01 07:21

I\'m trying to make a UI for downloading files from my site. The site have zip-files and these need to be downloaded to the directory entered by the user. However, I can\'t

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 07:44

    Take a look at http://www.csharp-examples.net/download-files/ and msdn docs on webclient http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

    My suggestion is try the synchronous download as its more straightforward. you might get ideas on whether webclient parameters are wrong or the file is in incorrect format while trying this.

    Here is a code sample..

    private void btnDownload_Click(object sender, EventArgs e)
    {
      string filepath = textBox1.Text;
      WebClient webClient = new WebClient();
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
      webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), filepath);
    }
    
    private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
      progressBar.Value = e.ProgressPercentage;
    }
    
    private void Completed(object sender, AsyncCompletedEventArgs e)
    {
      MessageBox.Show("Download completed!");
    }
    

提交回复
热议问题