我正在将图像上传到 cloudinary onChange,但在将响应图像 URL 保存为状态数组或对象时遇到挑战

发布于 2025-01-17 17:26:45 字数 1266 浏览 0 评论 0 原文

上传我的图像后,我的响应返回将有安全的图像URL。但是,我想将URL保存为图像URL的数组或对象,因为它们是我的产品库。

 const multipleFilesUpload = async (files) => {
    const formData = new FormData();

    for (let i = 0; i < files.length; i++) {
      let file = files[i];
      formData.append("file", file);
      formData.append("upload_preset", "kwingy");

      axios
        .post("https://api.cloudinary.com/v1_1/kwjsjsy/image/upload", formData)
        .then((response) => {
          console.log(response.data.secure_url);
        });
    }
  };

将图像上传时,将其记录到多个图像的URL。 因为我想将其传递给我的数据库

我如何将URL作为状态数组或对象保存, ://res.cloudinary.com/kwingy/image/upload/v1648607946/tudifdsk2j0dy77tpumze.png“ rel =“ nofollow noreferrer”> https://res.cloudinary.clinary.com/kwingy/image/image/image/upload/v166464646464644tpynptnypnempen g < /a> index.tsx:48:18 index.tsx:48:18 index.tsx:48:18

After my images have been uploaded my response returns secured images urls. However i want to save the urls as an array or object of image urls as they are for my products gallery.

 const multipleFilesUpload = async (files) => {
    const formData = new FormData();

    for (let i = 0; i < files.length; i++) {
      let file = files[i];
      formData.append("file", file);
      formData.append("upload_preset", "kwingy");

      axios
        .post("https://api.cloudinary.com/v1_1/kwjsjsy/image/upload", formData)
        .then((response) => {
          console.log(response.data.secure_url);
        });
    }
  };

When the images are uploaded its logging the urls of the multiple images. How do i save the urls as a state array or object as i will want to pass it to my database later on.......

below is my console log response

https://res.cloudinary.com/kwingy/image/upload/v1648607946/tudifdsk2j0dy7tpumze.png index.tsx:48:18
https://res.cloudinary.com/kwingy/image/upload/v1648607947/f8ylw5iwlaajzhjqr5ld.png index.tsx:48:18
https://res.cloudinary.com/kwingy/image/upload/v1648607946/kpyqcxxgysumahggerom.png index.tsx:48:18

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

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

发布评论

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

评论(1

海拔太高太耀眼 2025-01-24 17:26:45

您可以重构代码以对上传进行排队,同时运行它们,然后拉出安全链接并将它们写入数据库。像这样:

  const multipleFilesUpload = async (files) => {
    const formData = new FormData();

    const postPromises = []
    for (let i = 0; i < files.length; i++) {
      let file = files[i];
      formData.append("file", file);
      formData.append("upload_preset", "kwingy");
      postPromises.push(axios.post("https://api.cloudinary.com/v1_1/kwjsjsy/image/upload", formData))
    }
    try {
      const results = await Promise.all(postPromises)
      // Now pull out the urls and write to your DB...
      console.log('secure_urls', results.map(({data:{secure_url}})=>secure_url))
    } catch (error) {
        // if any of the uploads fail, they all fail
        console.log(error)
    }
  }

您可以将值放入状态变量中:

// state variable to hold your values
const [secureUrls] = useState([])

const multipleFilesUpload = async (files) => {
    const formData = new FormData();

    for (let i = 0; i < files.length; i++) {
      let file = files[i];
      formData.append("file", file);
      formData.append("upload_preset", "kwingy");

      axios
        .post("https://api.cloudinary.com/v1_1/kwjsjsy/image/upload", formData)
        .then((response) => {
          console.log(response.data.secure_url);
          // add url to the array of urls
          secureUrls.push(response.data.secure_url)
        });
    }
  };

You could restructure the code to queue up the uploads, run them concurrently, then pull out the secure links and write them to your DB. Something like this:

  const multipleFilesUpload = async (files) => {
    const formData = new FormData();

    const postPromises = []
    for (let i = 0; i < files.length; i++) {
      let file = files[i];
      formData.append("file", file);
      formData.append("upload_preset", "kwingy");
      postPromises.push(axios.post("https://api.cloudinary.com/v1_1/kwjsjsy/image/upload", formData))
    }
    try {
      const results = await Promise.all(postPromises)
      // Now pull out the urls and write to your DB...
      console.log('secure_urls', results.map(({data:{secure_url}})=>secure_url))
    } catch (error) {
        // if any of the uploads fail, they all fail
        console.log(error)
    }
  }

You could put the values into a state variable:

// state variable to hold your values
const [secureUrls] = useState([])

const multipleFilesUpload = async (files) => {
    const formData = new FormData();

    for (let i = 0; i < files.length; i++) {
      let file = files[i];
      formData.append("file", file);
      formData.append("upload_preset", "kwingy");

      axios
        .post("https://api.cloudinary.com/v1_1/kwjsjsy/image/upload", formData)
        .then((response) => {
          console.log(response.data.secure_url);
          // add url to the array of urls
          secureUrls.push(response.data.secure_url)
        });
    }
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文