[英]Get url of multi images when upload them to Firebase Storage
你能帮我在将图像上传到 firebase 时获取 URL 数组吗?
上传 2 img 时我的 imageUrl = [undefined, undefined]
export const handleUploadProductPhoto = async (files) => {
const imageUrl = await Promise.all(
files.map((file) => {
new Promise((resolve, reject) => {
const uploadTask = firebase
.storage()
.ref()
.child(`your/file/path/${file.name}`)
.put(file);
uploadTask.on(
firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
const progress =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
if (snapshot.state === firebase.storage.TaskState.RUNNING) {
console.log(`Progress: ${progress}%`);
}
},
(error) => console.log(error),
() => {
uploadTask.snapshot.ref
.getDownloadURL()
.then((url) => resolve(url)); //I have success here - two urls
}
);
});
})
).then((res) => console.log(res));
};
您缺少new Promise
的return
。 所以应该是:
return new Promise((resolve, reject) => {
...
如果没有那个return
,每个files.map((file) => {
什么都不返回(所以undefined
),这就是Promise.all
然后传递给你的then
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.