file upload using axios in react

Viewed 3970

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

alert(values.attachedFile[0]);

enter image description here

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.

enter image description here

what mistake I am doing?

1 Answers

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'
    }
})
Related