繁体   English   中英

上传多张图片到Firebase存储得到url张

[英]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 Promisereturn 所以应该是:

return new Promise((resolve, reject) => {
  ...

如果没有那个return ,每个files.map((file) => {什么都不返回(所以undefined ),这就是Promise.all然后传递给你的then

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM