VB - 使用 DownloadFileASync (WebClient) 进行多次下载

发布于 2024-12-12 18:41:34 字数 1400 浏览 0 评论 0原文

我正在尝试根据用户在表单上选择的内容下载多个文件。 我有多个复选框,因此如果用户选择复选框 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2024-12-19 18:41:34

我认为是因为您使用单个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文