我想在我的Xamarin.Forms中创建一个下载按钮,点击后,应用程序将从我的FirebaseStorage的特定URL下载一个文件(pdf/pptx/docx(到我的设备的内部存储,并在通知栏中显示下载过程。
如何将其存档在Xamarin.Forms中?
我有下面的按钮代码:
private async void DownloadButton_Clicked(object sender, EventArgs e)
{
var fc = new FirebaseStorage("appspot.com");
var getFileUrl = await
fc.Child("NonImage").Child("Pdf").Child("test.pdf").GetDownloadUrlAsync();
// what should I do next to archive what I described?
}
编辑:我尝试了HttpCient和WebClient,但我得到了一些错误
HttpClient:
private async void HttpHelper()
{
var fc = new FirebaseStorage("appspot.com");
var getFileUrl = await fc.Child("Image")
.Child("User").Child("test.pdf").GetDownloadUrlAsync();
string GetRootPath = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal);
string Complete = Path.Combine(GetRootPath, "test.pdf");
byte[] fileBytes = await _httpClient.GetByteArrayAsync(new
Uri(getFileUrl));
File.WriteAllBytes(Complete, fileBytes);
File.ReadAllBytes(Complete);
}
WebClient:
private async void HttpHelper()
{
var fc = new FirebaseStorage(".appspot.com");
var getFileUrl = await fc.Child("Image")
.Child("User").Child("test.pdf").GetDownloadUrlAsync();
using (var client = new WebClient())
{
client.DownloadFile(getFileUrl, "test.pdf");
}
}
执行两个功能时出现错误:
显式并发复制GC释放了3(16KB(AllocSpace对象,0(0B(LOS对象,49%的可用空间,3014KB/6028KB,暂停84us总计21.880ms
[Surface]操作服务为空错误
使用GetDownloadUrlAsync获取文件的下载URL。
var getFileUrl = await fc.Child("Image")
.Child("User").Child("test.pdf").GetDownloadUrlAsync();
然后使用下载管理器从URL下载文件。
DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(url));
request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
request.SetDestinationInExternalFilesDir(Forms.Context, Android.OS.Environment.DirectoryDownloads, "hello.jpg");
DownloadManager dm = (DownloadManager)Android.App.Application.Context.GetSystemService(Android.App.Application.DownloadService);
dm.Enqueue(request);
DownloadManager提供自己的通知来跟踪您的下载进度。