通过节点将base64图像从react-webcam上传到cloudinary

问题描述 投票:1回答:1

我是新来的反应者,感谢您的帮助。当通过Node将jpg上载到cloudinary时,我有以下适用的代码。

onChange中调用以下方法;

uploadProfilePic = e => {
  const files = Array.from(this.state.profilePic)
  const formData = new FormData()

  files.forEach((file, i) => {
    formData.append(i, file)
  })
    fetch(`http://localhost:3030/imageUpload`, {
      method: 'POST',
      body: formData
    })
    .then(res => res.json())
    .then(images => {
      this.setState({profilePic: images[0].url})
    })
  }

以及在我的server.js文件中;

  const values = Object.values(req.files)
    const promises = values.map(image => cloudinary.uploader.upload(image.path))

    Promise
      .all(promises)
      .then(results => res.json(results))
  })

以上已成功将jpg上传到cloudinary,并按预期将URL设置为我的状态。

只是想知道如何调整上面的2个代码块,以便能够上传通过存储在我的状态{this.state.passImage}中的react-webcam捕获的base64图像*,以实现相同的结果(又名上传到cloudinary并检索URL)?

到目前为止,我已经尝试过

uploadPassImage= e => {

const formData = JSON.stringify(this.state.passImage)

    fetch(`http://localhost:3030/imageUploadPassImage`, {
      method: 'POST',
      body: formData
    })
    .then(res => res.json())
    .then(images => {
      this.setState({passImage: images[0].url})
    })
  }

带有服务器代码;

  app.post('/imageUploadPassImage', (req, res) => {
    const values = Object.values(req.files)
      const promises = values.map(image => cloudinary.uploader.upload(image.path))

      Promise
        .all(promises)
        .then(results => res.json(results))
    })

没有运气。

javascript node.js reactjs express cloudinary
1个回答
0
投票

我知道了。对于任何想知道或遇到相同问题的人,请在此处发布。

on react

uploadPassImage= e => {

const formData = JSON.stringify(this.state.passImage)

    fetch(`http://localhost:3030/imageUploadPassImage`, {
      method: 'POST',
      body: formData
    })
    .then(res => res.json())
    .then(images => {
      this.setState({passImage: images[0].url})
    })
  }

在服务器上;

app.post('/imageUploadPassImage', (req, res) => {
    const values = Object.values(req.files)
      const promises = values.map(image => cloudinary.uploader.upload(image.path))

      Promise
        .all(promises)
        .then(results => res.json(results))
    })

© www.soinside.com 2019 - 2024. All rights reserved.