离线上传文件到 Firebase 云存储时触发代码
Trigger Code When Uploading a File to Firebase Cloud Storage When Offline
这是 Firebase 在其文档中使用的代码,作为将文件上传到 Firebase 云存储的参考。
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() 方法中制作)?
我假设离线时,在第一个 'running' 案例被触发后,下一个 'state_changed' 更新将是关于没有互联网连接并且 运行 'pause' 案件。在这种情况下,我想编写一些代码以在离线上传文件的情况下执行。
然而,这不会发生,并且 'pause' 情况在离线或连接中途丢失时永远不会 运行s。错误回调也没有运行。代码只是挂在那里,直到可以建立连接。
在 uploadTask.on() 方法中,离线时只需要 运行 执行代码的标准方法是什么(比如将文件上传到本地存储,直到可以建立连接) .
在此先感谢您的帮助!
用于云存储的 Firebase SDK 不将没有互联网连接视为错误条件。相反,它只会重试指数退避的操作。
据我所知,pause
事件仅在您自己调用 pause()
函数时触发,并且可以在 SDK source.
中看到
这是 Firebase 在其文档中使用的代码,作为将文件上传到 Firebase 云存储的参考。
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() 方法中制作)?
我假设离线时,在第一个 'running' 案例被触发后,下一个 'state_changed' 更新将是关于没有互联网连接并且 运行 'pause' 案件。在这种情况下,我想编写一些代码以在离线上传文件的情况下执行。 然而,这不会发生,并且 'pause' 情况在离线或连接中途丢失时永远不会 运行s。错误回调也没有运行。代码只是挂在那里,直到可以建立连接。
在 uploadTask.on() 方法中,离线时只需要 运行 执行代码的标准方法是什么(比如将文件上传到本地存储,直到可以建立连接) .
在此先感谢您的帮助!
用于云存储的 Firebase SDK 不将没有互联网连接视为错误条件。相反,它只会重试指数退避的操作。
据我所知,pause
事件仅在您自己调用 pause()
函数时触发,并且可以在 SDK source.