【问题标题】:Not able to POST image files to server无法将图像文件发布到服务器
【发布时间】:2021-12-06 15:56:58
【问题描述】:

我正在尝试使用 POST 请求将图像发送到服务器,但在服务器端我正在接收并清空对象。

  // Client Side

  function uploadFile(thumbnailRef) {
    const thumbnail = thumbnailRef.current.files[0];
    const formData = new FormData();
    formData.append('file', thumbnail)

   fetch('http://localhost:8080/upload', {
        method: 'POST',
        body: formData,
        headers: {
            'Content-Type': 'multipart/form-data'
        },

    }).then(res => { console.log(res) })
        .catch(err => console.log(err))

  }
  // server side

  app.post('/upload', (req, res) => {
    console.log(req.body) // getting empty - {}
    res.end()
})

【问题讨论】:

    标签: node.js reactjs express http


    【解决方案1】:

    我认为如果您删除标题,它应该可以工作。像这样:

    function uploadFile(thumbnailRef) {
        const thumbnail = thumbnailRef.current.files[0];
        const formData = new FormData();
        formData.append('file', thumbnail)
    
       fetch('http://localhost:8080/upload', {
            method: 'POST',
            body: formData,
    
        }).then(res => { console.log(res) })
            .catch(err => console.log(err))
    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      相关资源
      最近更新 更多