如何将图像列表上传到Firebase存储并获取所有URL

发布于 2025-02-08 12:39:39 字数 670 浏览 2 评论 0原文

我正在使用Firebase存储上传图像,然后将URL存储到Firebase DB中。 我正在使用它来上传图像列表,但是下面的代码在firebase V8中效果很好,但在版本9中却没有

export const multiImageUpload = async (path = "Products", imageFileList) => {
  let imagesUrlArray = [];
  let imageUrl = "";
  // array of files
  let arr = imageFileList.map((item) => {
    return item.originFileObj;
  });

  for (let i = 0; i < arr.length; i++) {
    const upload = await storage.ref(`/${path}/${arr[i].name}`).put(arr[i]);
    imageUrl = await upload.ref.getDownloadURL();
    imagesUrlArray.push(imageUrl);
  }

  return imagesUrlArray; // array of URLS of uploaded files
};

引发错误,ref.put不是函数 请指导我为什么我会遇到这个错误。

I am using firebase storage to upload the images and then store the urls to firebase db.
I am using that to upload the list of images but the code below works fine in firebase v8 but not in version 9

export const multiImageUpload = async (path = "Products", imageFileList) => {
  let imagesUrlArray = [];
  let imageUrl = "";
  // array of files
  let arr = imageFileList.map((item) => {
    return item.originFileObj;
  });

  for (let i = 0; i < arr.length; i++) {
    const upload = await storage.ref(`/${path}/${arr[i].name}`).put(arr[i]);
    imageUrl = await upload.ref.getDownloadURL();
    imagesUrlArray.push(imageUrl);
  }

  return imagesUrlArray; // array of URLS of uploaded files
};

it throws error that ref.put is not a function
please guide me why I am getting that error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2025-02-15 12:39:39

导入{ref,get downloadurl,uploadBytesresumable,uploadBytes} from'firebase/storage';

// Upload the multiple images
        const multiImage = async (imageFileList) => {
            /* eslint no-var: 0 */
            const imagesUrlArray = [];
            /* eslint no-var: 0 */
            for (let i = 0; i < imageFileList.length; i++) {
                const id = uuidv4();
                const metadata = {
                    contentType: 'image/jpeg'
                };
                /* eslint-disable no-await-in-loop */
                const storageRef = ref(storage, `images/${id}`);
                const upload = await uploadBytes(storageRef, imageFileList[i], metadata);
                const imageUrl = await getDownloadURL(storageRef);
                imagesUrlArray.push(imageUrl);
            }
            return imagesUrlArray; // array of URLS of uploaded files
        };

import { ref, getDownloadURL, uploadBytesResumable, uploadBytes } from 'firebase/storage';

// Upload the multiple images
        const multiImage = async (imageFileList) => {
            /* eslint no-var: 0 */
            const imagesUrlArray = [];
            /* eslint no-var: 0 */
            for (let i = 0; i < imageFileList.length; i++) {
                const id = uuidv4();
                const metadata = {
                    contentType: 'image/jpeg'
                };
                /* eslint-disable no-await-in-loop */
                const storageRef = ref(storage, `images/${id}`);
                const upload = await uploadBytes(storageRef, imageFileList[i], metadata);
                const imageUrl = await getDownloadURL(storageRef);
                imagesUrlArray.push(imageUrl);
            }
            return imagesUrlArray; // array of URLS of uploaded files
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文