簡體   English   中英

如何在 Xamarin.Forms 中將非圖像文件(pdf、pptx、docx)從 FirebaseStorage 下載到設備的內部存儲?

[英]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,但出現一些錯誤

客戶端:

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 is 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 提供自己的通知來跟蹤您的下載進度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM