最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - file upload using axios in react - Stack Overflow

programmeradmin0浏览0评论

I am uploading file in react using axios. when I am doing

alert(values.attachedFile[0]);

but when I am sending values.attachedFile[0] in axios post request enpty thing is going.

  const { result } = await axios.post(app.resourceServerUrl + '/file/upload', {
        data: values.attachedFile[0],
        headers: {
            'Content-Type': 'multipart/form-data',
        },
    });

but as part of request is is going empty.

what mistake I am doing?

I am uploading file in react using axios. when I am doing

alert(values.attachedFile[0]);

but when I am sending values.attachedFile[0] in axios post request enpty thing is going.

  const { result } = await axios.post(app.resourceServerUrl + '/file/upload', {
        data: values.attachedFile[0],
        headers: {
            'Content-Type': 'multipart/form-data',
        },
    });

but as part of request is is going empty.

what mistake I am doing?

Share Improve this question edited Feb 15, 2022 at 7:55 Shruti sharma asked Feb 8, 2022 at 14:09 Shruti sharmaShruti sharma 2119 gold badges32 silver badges89 bronze badges 1
  • Show source code of nest.js controller who manage the route file/upload – Fiodorov Andrei Commented Feb 15, 2022 at 8:14
Add a comment  | 

1 Answer 1

Reset to default 14 +100

To upload file with axios you need to use FormData:

const formData = new FormData();

// ...

formData.append("data", values.attachedFile[0]);
axios.post(app.resourceServerUrl + '/file/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})
发布评论

评论列表(0)

  1. 暂无评论