離線時將文件上傳到 Firebase 雲存儲時觸發代碼

丹尼爾·布朗

這是 Firebase 在其文檔中用作將文件上傳到 Firebase Cloud Storage 的參考的代碼。

import { getStorage, ref, uploadBytesResumable, getDownloadURL } from "firebase/storage";

const storage = getStorage();
const storageRef = ref(storage, 'images/rivers.jpg');

const uploadTask = uploadBytesResumable(storageRef, file);

// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', 
  (snapshot) => {
    // Observe state change events such as progress, pause, and resume
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case 'paused':
        console.log('Upload is paused');
        break;
      case 'running':
        console.log('Upload is running');
        break;
    }
  }, 
  (error) => {
    // Handle unsuccessful uploads
  }, 
  () => {
    // Handle successful uploads on complete
    // For instance, get the download URL: https://firebasestorage.googleapis.com/...
    getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
      console.log('File available at', downloadURL);
    });
  }
);

TLTR:在uploadTask.on() 方法中執行只需要在離線時運行的代碼的標準方法是什麼(比如將文件上傳到本地存儲,直到可以建立連接)?

我假設離線時,在第一個“運行”案例被觸發後,下一個“state_changed”更新將是關於沒有互聯網連接並且會運行“暫停”案例。在這種情況下,我想編寫一些代碼以在離線時上傳文件的情況下執行。然而,這不會發生,並且“暫停”情況永遠不會在離線或中途失去連接時運行。錯誤回調也不會運行。代碼只是掛在那裡,直到可以建立連接。

在 uploadTask.on() 方法中執行只需要在離線時運行的代碼的標準方法是什麼(比如將文件上傳到本地存儲,直到可以建立連接)。

在此先感謝您的幫助!

弗蘭克·範·普費倫

Firebase SDK for Cloud Storage 不會將沒有互聯網連接視為錯誤情況。相反,它只會重試指數退避的操作。

據我所知,pause只有在您pause()自己調用函數時才會觸發事件,並且可以在SDK 源代碼中看到

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 firebase 函數將時間戳發送到 firebase

使用 curl 將 fly 上傳到 FastAPI 端點 - 307 臨時重定向

當我們將數據從加密的 AWS S3 存儲桶發送到加密的 Google Cloud Storage 存儲桶時,該數據在傳輸過程中是否加密?

在將文件上傳到 Amazon S3 時創建子文件夾

在為每個模型的不同 DAL 類中實現 JPA 存儲庫方法時如何避免重複的代碼行/代碼塊

在 HDFS 中存儲 TsFile 時,IoTDB 中的 Compaction Merge 發生 UnsupportedOperationException

如何使用帶有 Nuxt.js 和 Axios 的預簽名 url 將文件上傳到 S3 存儲桶?

開發服務器返迴響應錯誤代碼:500 in react native 當通過安裝 firebase 連接到 firebase 時

如何將大文件上傳到 Azure Blob 存儲 (.NET Core)

如何從雲函數將圖像存儲在 Firebase 存儲上

將代碼從控制器傳輸到存儲庫無法正常工作

當我將文件上傳到 Android 中的 Firebase 存儲時,進度條未更新

從 Firebase 函數獲取雲存儲中文件的永久可公開下載的 URL

將所有大學項目上傳到一個存儲庫

將 base64 上傳到 firebase 存儲並取回下載 url

使用指定的 ContentType 將 Blob 上傳到 Azure Blob 存儲並同時覆蓋(.NET v12 SDK)?

從 ReactJS 上傳圖片到 Firebase v9 存儲

使用 React 將文件上傳到 AWS S3 時使用進度處理程序

使用 Laravel 8 將圖像上傳到存儲和數據庫時出現的問題

將圖像同時上傳到 firebase 存儲和文本輸入到 firestore 時出錯

將代碼推送回 Azure Devops 上的 TFVC 存儲庫

“GetObject 時發生錯誤。S3 錯誤代碼:PermanentRedirect。S3 錯誤消息:存儲桶在此區域:us-east-1

如何將從firebase存儲上傳的圖像保存到firestore數據庫中的currentUser

嘗試通過反應上傳到 Firebase 存儲。類型錯誤:無法讀取未定義的屬性(讀取 'ref')

使用 Firebase v9 從網站上傳文件到 Firebase 存儲

如何在Java Spring Boot的firebase存儲中獲取上傳文件的downloadURL

通過 Azure Front Door 將文件上傳到 Blob 存儲

如何通過 Apache Beam 將文件上傳到 Azure blob 存儲?

Firebase 函數有時會觸發 onDelete 工作