使用 C# Windows Forms 和 webclient 下载文件
Posted
技术标签:
【中文标题】使用 C# Windows Forms 和 webclient 下载文件【英文标题】:Download files with C# Windows Forms and webclient 【发布时间】: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.Cancelled
msdn.microsoft.com/en-us/library/…
【参考方案1】:
对于这种情况,如果您只想下载文件,您可能需要进行同步下载调用,而不是像您尝试实现的异步调用。
这可以通过类似的方式完成:
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!");
我个人会在这种情况下使用同步调用,直到您更好地了解异步调用以及它们之间的优缺点。
【讨论】:
很好的解释。谢谢! 在我将文件名放入文件路径字符串之前,我仍然无法使其正常工作。有没有一种方法可以使下载的文件自动命名为与服务器上的文件相同的名称? 您是否有理由不能以如下方式注释文件名:txtBxSaveTo.Text.ToString() + "10MB.zip"
?我很好奇,因为您似乎在硬编码服务器上文件的路径。
据我所知,将其包含在示例中似乎并不谨慎,因为这似乎与原始问题无关。【参考方案2】:
下载文件方法抛出异常。另外......在开始异步之前使用非异步版本开始。
"远程服务器返回错误:(407) 需要代理验证。"
【讨论】:
这与我遇到的问题相同。任何线索如何克服这个?以上是关于使用 C# Windows Forms 和 webclient 下载文件的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Visual Studio 2019 中使用 Xamarin Forms(UWP) 和 C# 创建 Web 应用程序
在 Parallels 8 中的 Windows 7 上运行 C# Windows Forms 应用程序错误
无法在 C# 中将类型“System.EventHandler”隐式转换为“System.Windows.Forms.KeyPressEventHandler”[重复]