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 |1 Answer
Reset to default 14 +100To 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'
}
})
file/upload
– Fiodorov Andrei Commented Feb 15, 2022 at 8:14