我正在学习如何在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;
}
}
}
}
答案 0 :(得分:2)
对于这种情况,如果您只是想下载该文件,您可能希望进行同步下载调用而不是像您尝试实现的异步调用。
这可以通过与此类似的方式完成:
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);
}
这将锁定程序,直到文件下载或发生错误,但您是否特别需要异步下载?如果您这样做,可能会有以下解释:
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!");
}
我个人会在这种情况下使用同步调用,直到你更好地理解异步调用及它们之间的利弊。
答案 1 :(得分:0)
下载文件方法抛出异常。另外......在转向异步之前,请先使用非异步版本。
{“远程服务器返回错误:(407)需要代理验证。”}