你们真的很亲密。根据API参考 https://firebase.google.com/docs/reference/js/v8/firebase.storage.Reference#put,第二个参数是UploadMetadata object https://firebase.google.com/docs/reference/js/v8/firebase.storage.UploadMetadata它有一个名为customMetadata https://firebase.google.com/docs/reference/js/v8/firebase.storage.UploadMetadata#optional-custommetadata.
因此,要更正您的代码,您需要换出
myRef.put(file, { metadata: metadata });
with
wmyRef.put(file, { customMetadata: metadata });
此外,您的进度逻辑不应调用resolve
and reject
就像现在一样——那就是反模式 https://stackoverflow.com/questions/23803743。你应该改为链接到UploadTask
自己的 Promise API。
return wmyRef.put(file, { customMetadata: metadata })
.then(
(snapshot) => { // onComplete
return snapshot.ref.getDownloadURL();
},
(err) => { // onError (although I would just omit this entirely)
console.log("failed to upload", err);
throw err;
}
);
使用您的原始代码,如果getDownloadURL()
失败,其错误将不会被处理。
如果您仍然需要进度报告,请使用:
const uploadTask = wmyRef.put(file, { customMetadata: metadata });
uploadTask.on(
"state_changed",
(snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log("Upload is " + progress + "% done");
}
);
return uploadTask
.then(snapshot => {
console.log("Upload is complete");
return snapshot.ref.getDownloadURL()
});