【发布时间】:2015-05-12 19:31:02
【问题描述】:
我正在学习如何在 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;
}
}
}
}
【问题讨论】:
-
在您的
Completed处理程序中,尝试检查e.Error和e.Cancelledmsdn.microsoft.com/en-us/library/…