VB - 使用 DownloadFileASync (WebClient) 进行多次下载
我正在尝试根据用户在表单上选择的内容下载多个文件。 我有多个复选框,因此如果用户选择复选框 1、3、4,我希望网络客户端下载文件 1.txt、3.txt、4.txt。 WebClient 方法导致“WebClient 不支持并发 I/O 操作”。错误。
If chk1.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/1.txt), Path.Combine(DataSource & strDirectory, "1.txt"))
End If
If chk2.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/2.txt), Path.Combine(DataSource & strDirectory, "2.txt"))
End If
If chk3.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/3.txt), Path.Combine(DataSource & strDirectory, "3.txt"))
End If
If chk4.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/4.txt), Path.Combine(DataSource & strDirectory, "4.txt"))
End If
我确实有一个跟踪下载的进度条,以及一个调用消息框的已完成事件。
Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub WC_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
MessageBox.Show("Download complete", "Download", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
我怎样才能对此进行编程以下载所有检查的文件? 我不介意用户是否只看到显示下载的总文件或剩余时间的进度条,但我确实需要在所有下载完成时显示一些内容。
有什么建议吗?
I'm trying to download multiple files based on what a user has selected on a form.
I have multiple checkboxes in place, so If a user would select Checkboxes 1,3,4 I would want the webclient to download files 1.txt, 3.txt, 4.txt.
The WebClient method is causing a "WebClient does not support concurrent I/O operations." error.
If chk1.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/1.txt), Path.Combine(DataSource & strDirectory, "1.txt"))
End If
If chk2.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/2.txt), Path.Combine(DataSource & strDirectory, "2.txt"))
End If
If chk3.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/3.txt), Path.Combine(DataSource & strDirectory, "3.txt"))
End If
If chk4.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/4.txt), Path.Combine(DataSource & strDirectory, "4.txt"))
End If
I do have a progress bar that tracks the download, as well as a completed event that calls a messagebox.
Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub WC_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
MessageBox.Show("Download complete", "Download", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
How could I go about programming this to download all files checked?
I don't mind if the user only sees that progress bar showing total files downloaded or time remaining, but I do need something to show when all downloads are completed.
Any Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为是因为您使用单个 WebClient 实例同时执行多个 HTTP 请求。尝试使用多个实例。
I think is because of you using single WebClient instance to execute several HTTP requests at the same time. Try to use several instances.