【发布时间】:2014-08-18 20:45:49
【问题描述】:
我有一个 zip 文件创建器,它接收一个 String[] 的 Urls,并返回一个包含 String[] 中所有文件的 zip 文件
我想会有很多这样的例子,但我似乎找不到“如何异步下载许多文件并在完成后返回”的答案
如何一次下载 {n} 个文件,并仅在所有下载完成后返回字典?
private static Dictionary<string, byte[]> ReturnedFileData(IEnumerable<string> urlList)
{
var returnList = new Dictionary<string, byte[]>();
using (var client = new WebClient())
{
foreach (var url in urlList)
{
client.DownloadDataCompleted += (sender1, e1) => returnList.Add(GetFileNameFromUrlString(url), e1.Result);
client.DownloadDataAsync(new Uri(url));
}
}
return returnList;
}
private static string GetFileNameFromUrlString(string url)
{
var uri = new Uri(url);
return System.IO.Path.GetFileName(uri.LocalPath);
}
【问题讨论】:
标签: c# .net async-await webclient