【问题标题】:Why won't this react js file uploader work?为什么这个反应js文件上传器不起作用?
【发布时间】:2019-10-30 15:52:27
【问题描述】:

我正在尝试从我的反应前端将图像文件发送到我的后端 express.js api。但是在我的 console.log 中,我只是在后端得到一个看起来像 {} 的空对象。 react.js api调用有什么问题,不会让我在后端的console.log中看到文件。

//后端接口

app.post('/api/v1/media', function (req, res) {
  console.log(req.body);
  res.json('api called');
 });

//React JS api call

const files = e.target.files;
console.log(files);

    setLoading(true);
    const res = await fetch(
      'http://localhost:4000/api/v1/media',
      {
        method:"POST",
        headers: {'Content-Type':'application/json'},
        body: JSON.stringify(files[0])
      }`enter code here`
    )
    const file = await res.json();

【问题讨论】:

  • 尝试添加"Accept": "application/json"

标签: node.js reactjs file-upload


【解决方案1】:

尝试使用

"Content-type" : "multipart/form-data"

另请参阅this帖子以获取文件上传参考。

【讨论】:

  • 我试试看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 2021-05-27
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
相关资源
最近更新 更多