首页 > 解决方案 > 为什么这反应js文件上传器不起作用?

问题描述

我正在尝试从我的反应前端将图像文件发送到我的后端 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();

标签: node.jsreactjsfile-upload

解决方案


尝试使用

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

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


推荐阅读