为什么此反应js文件上传器不起作用?

时间:2019-10-30 15:52:27

标签: node.js reactjs file-upload

我正在尝试从我的前端向我的后端express.js api发送图像文件。但是在我的console.log中,我只是在后端获得了一个空对象,看起来像{}。 react.js api调用有什么问题,它使我无法在后端的console.log中看到文件。

//后端API

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();

1 个答案:

答案 0 :(得分:1)

尝试使用

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

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

相关问题