如何从 FirebaseStorage 下载非图像文件(pdf、pptx、docx)到 Xamarin.Forms 中的设备内部存储?

How to download non-image file(pdf,pptx,docx) from FirebaseStorage to internal storage of device in Xamarin.Forms?

我想在我的 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);
 }

网络客户端:

    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] opservice 为 null false

使用 GetDownloadUrlAsync 获取文件的下载URL。

 var getFileUrl = await  fc.Child("Image") 
    .Child("User").Child("test.pdf").GetDownloadUrlAsync();

然后使用 DownloadManager 从 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 提供自己的通知来跟踪您的下载进度。