【发布时间】:2021-10-06 15:36:38
【问题描述】:
我正在尝试编写一个函数,将 jpeg 图像上传到 Firebase 存储并返回上传文件的下载链接。但是当我执行该函数时,文件并没有完全上传到存储中,即原始文件大小为 10 KB,上传文件大小为 9 B。我正在使用以下代码:
const storageRef = firebase.storage().ref();
const dpRef = storageRef.child("users/dp.jpg").put(file, {
contentType: "image/jpeg",
});
dpRef.on(
firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
(snapshot) => {
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log("Upload is " + progress + "% done");
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log("Upload is paused");
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log("Upload is running");
console.log(progress);
break;
default:
break;
}
},
(error) => {
// A full list of error codes is available at
// https://firebase.google.com/docs/storage/web/handle-errors
switch (error.code) {
case "storage/unauthorized":
// User doesn't have permission to access the object
console.log("Unauthorized");
break;
case "storage/canceled":
// User canceled the upload
console.log("Cancelled");
break;
// ...
case "storage/unknown":
// Unknown error occurred, inspect error.serverResponse
console.log("Unknown");
break;
default:
break;
}
},
() => {
// Upload completed successfully, now we can get the download URL
dpRef.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log("File available at", downloadURL);
});
}
);
console.log(progress) 出于某种原因显示 NaN。我也试过下载
【问题讨论】:
标签: javascript google-cloud-storage firebase-storage