如何查看下载的文件当使用Xamarin Android进行另一次下载时 [英] How to View the downloaded file When another download is in progress using Xamarin Android
问题描述
我正在使用Xamarin android实现移动应用程序.我实现了一个代码,只需单击一下即可下载.PDF和.Mobi文件.我使用了下面的代码.
I am implementing a mobile app using Xamarin android. I have implemented a code to download both.PDF and .Mobi file in a button click. I have used the below code.
...
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();
}
}
它同时下载两个文件.我想首先下载PDF文件.下载PDF文件后,按钮文本应从下载"更改为查看PDF".单击查看PDF时,应在PDF阅读器中打开文件.移动文件下载应在此过程之后开始,并且下载应在后台进行. 有人可以提出您的想法来实现这一目标吗?
Its download both file at a same time. I want to download the PDF file at first. Once PDF file has been downloaded the button text should be changed to "View PDF" from "Download". When click View PDF the the file should be opened in PDF reader. The Mobile file download should start after this process and download should be in background. Can you anyone suggest your ideas to achieve this?
推荐答案
private stringBuilder urlStr = null;
public void DownloadFiles()
{
List<string> url = new List<string>();
urlStr = new StringBuilder();
url.add("http://files/file.pdf");
url.add("http://files/file.mobi");
var tasks = new List<Task>();
foreach(var tempUrl in url)
{
tasks.add(DownloadMobiAndPdf(tempUrl);
}
Task.WhenAll(tasks));
}
private async Task DownloadMobiAndPdf(string url)
{
using(var client = new WebClient())
{
urlStr.Append(url);
await client.DonwloadFileTaskAsync(url);
client.DownloadFileCompleted+=Client_DownloadFileCompleted;
}
}
private static void Client_DownloadFileCompleted(object
sender,System.ComponentModel.AsyncCompletedEventArgs e)
{
if(e.Error == null)
{
//No error
if(urlStr.Contains("pdf")
{
//Enable button
}
}
}
这篇关于如何查看下载的文件当使用Xamarin Android进行另一次下载时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!