DownloadFileAsync:多个文件
我在使用 C# WebClient DownloadFileAsync 时遇到了一些问题,希望您能帮助我。
这是我的场景:
我同时下载许多文件(http://example.com/abc/1.jpg,http://example.com/abc/efg/2.jpg 等)使用 WebClient DownloadFileAsync。
我当前的示例代码是:
while (theres still files in http://example.com/abc/) {
// filename will be something like /abc/1.jpg, /abc/efg/2.jpg
if (!file.exists(directory.getcurrentdirectory()+filename.Replace('/', '\\'))) {
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadCompleted);
webClient.DownloadFileAsync(new Uri("http://example.com"+filename), Directory.GetCurrentDirectory()+filename.Replace('/', '\\'));
}
如何使所有正在下载的文件显示在一个进度栏中?
例如。 1.jpg为50kb,2.jpg为50kb,当1.jpg下载完成时进度条将显示50%,2.jpg将在进度条中从51%到100%。
另外,如果文件名是/abc/1.jpg,如果我的当前目录没有名为abc的文件夹,则下载将无法进行。如何让它根据文件名自动创建文件夹? (例如/random123/3.jpg、/anotherrandom/4.jpg 等)
I have run into some problems using C# WebClient DownloadFileAsync and hope that you would help me.
Here's my scenario:
I am downloading many files at the same time (http://example.com/abc/1.jpg, http://example.com/abc/efg/2.jpg, etc.) using WebClient DownloadFileAsync.
My current example code is:
while (theres still files in http://example.com/abc/) {
// filename will be something like /abc/1.jpg, /abc/efg/2.jpg
if (!file.exists(directory.getcurrentdirectory()+filename.Replace('/', '\\'))) {
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadCompleted);
webClient.DownloadFileAsync(new Uri("http://example.com"+filename), Directory.GetCurrentDirectory()+filename.Replace('/', '\\'));
}
How do I make it so that all the files being downloaded is shown in one progress bar?
eg. 1.jpg is 50kb, 2.jpg is 50kb, when 1.jpg completes download the progress bar will show 50%, and 2.jpg will be from 51% to 100% in the progress bar.
Also if the filename is /abc/1.jpg, if my current directory do not have a folder called abc, the download will not work. How do I make it create the folder automatically depending on filename? (eg. /random123/3.jpg, /anotherrandom/4.jpg, etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要为您提供总体进度计数器,您需要一个集中例程,该例程收集每个正在进行的传输的到目前为止已完成的数量和要下载的总量,并将它们相加以计算总体总数。因为传输是并行运行的,所以您不能只将进度条的一部分(例如 51-100%)分配给每个文件。
对于大多数 API,您需要先为自己创建文件夹,然后再将文件写入其中。只需执行此操作即可在开始写入文件之前创建路径上所有丢失的文件夹:
To give an overall progress counter for you will need a centralised routine that gathers the amount-completed-so-far and total-amount-to-download for each of the in-progress transfers and sums them to calculate the overall totals. Because the transfers are running in parallel you can't just assign a chunk of the progress bar (e.g 51-100%) to each file.
With most APIs you will need to create folders for yourself before writing files into them. Just do this to create all missing folders on the path before you start writing the file:
要执行进度条操作,您可以使用队列。下载完成后,更新进度条并开始下一次下载。
但是,我认为 WebClient 类无法告诉您下载完成了多少,只有下载完成与否才知道。
要验证目录是否存在,请使用:
您可以使用 System.IO.Path 类从路径获取目录。
To do the progress bar thing, you can use a queue. When something finishes downloading, update the progress bar, and start the next download.
However, I don't think the WebClient class can tell you how much of the download finished, only if it is finished or not.
To verify if the directory exists, use:
You can get the directory from a path, using the System.IO.Path class.