使用 C# Windows Forms 和 webclient 下载文件

0 c# download winforms web

我正在学习如何在 C# windows 窗体中使用 http 请求和 webclient。目前,我已经从Example 中获得了以下代码,我正在努力使其工作并理解它。

代码成功执行并显示消息框“下载完成”框,但实际上并未下载文件。有人会向我解释这是如何工作的以及我做错了什么吗?

    private void btnDownload_Click(object sender, EventArgs e)
    {
        string filepath = txtBxSaveTo.Text.ToString();
        WebClient webClient = new WebClient();
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileAsync(new Uri("http://download.thinkbroadband.com/10MB.zip"), filepath);
    }

    private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
    }

    private void Completed(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("Download completed!");
    }

    private void btnSavetoLocation_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog selectedFolder = new FolderBrowserDialog();

        if (selectedFolder.ShowDialog() == DialogResult.OK)
        {
            txtBxSaveTo.Text = selectedFolder.SelectedPath;

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

小智 5

对于这种情况,如果您只想下载文件,您可能需要进行同步下载调用,而不是像您尝试实现的那样进行异步调用。

这可以通过与此类似的方式完成:

private void btnDownload_Click(object sender, EventArgs e)
{
    string filepath = txtBxSaveTo.Text.ToString();
    WebClient webClient = new WebClient();
    webClient.DownloadFile("http://download.thinkbroadband.com/10MB.zip", filepath);
}
Run Code Online (Sandbox Code Playgroud)

这将锁定程序,直到下载文件或发生错误,但您是否有特别需要异步下载?如果你这样做了,那么下面的解释可能会有用:

private void btnDownload_Click(object sender, EventArgs e)
{
  //Retrieve the path from the input textbox
  string filepath = txtBxSaveTo.Text.ToString();

  //Create a WebClient to use as our download proxy for the program.
  WebClient webClient = new WebClient();

  //Attach the DownloadFileCompleted event to your new AsyncCompletedEventHandler Completed
  //so when the event occurs the method is called.
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

  //Attach the DownloadProgressChanged event to your DownloadProgressChangedEventHandler ProgressChanged,
  //so again when the event occurs the method is called.
  webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

  //Attempt to actually download the file, this is where the error that you
  //won't see is probably occurring, this is because it checks the url in 
  //the blocking function internally and won't execute the download itself 
  //until this clears.
  webClient.DownloadFileAsync(new Uri("http://example.com/myfile.txt"), filepath);
}

//Method that just increments the progressBar every time the DownloadProgressChangedEvent from webClient fires.
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
  progressBar.Value = e.ProgressPercentage;
}

//This is your method that will pop when the AsyncCompletedEvent is fired,
//this doesn't mean that the download was successful though which is why
//it's misleading, it just means that the Async process completed.
private void Completed(object sender, AsyncCompletedEventArgs e)
{
  MessageBox.Show("Download completed!");
}
Run Code Online (Sandbox Code Playgroud)

我个人会在这种情况下使用同步调用,直到您更好地了解异步调用以及它们之间的优缺点。