我想使用Xamarin andorid按钮单击下载.pdf和.mobi文件。我们如何使用webclient触发多个文件下载?
答案 0 :(得分:3)
您可以使用HttpClient和Task.WhenAll
开始下载这两个文件,以便同时下载。
...
await Task.WhenAll(DownloadPDF(), DownloadMobi());
}
private async Task DownloadPDF()
{
var httpclient = new HttpClient(new AndroidClientHandler());
using (var stream = await httpclient.GetStreamAsync("http://files/file.pdf"))
using (var file = System.IO.File.Create("path/to/file.pdf"))
{
await stream.CopyToAsync(file);
await file.FlushAsync();
}
}
private async Task DownloadMobi()
{
var httpclient = new HttpClient(new AndroidClientHandler());
using (var stream = await httpclient.GetStreamAsync("http://files/file.mobi"))
using (var file = System.IO.File.Create("path/to/file.mobi"))
{
await stream.CopyToAsync(file);
await file.FlushAsync();
}
}