這是 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] 删除。
我来说两句