Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  36] [ 2]  / answers: 1 / hits: 11752  / 2 Years ago, tue, february 8, 2022, 12:00:00

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


alert(values.attachedFile[0]);

enter


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


what mistake I am doing?


More From » reactjs

 Answers
3

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

[#385] Thursday, February 3, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jocelynkarsynr

Total Points: 472
Total Questions: 98
Total Answers: 96

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
jocelynkarsynr questions
Sat, Jul 11, 20, 00:00, 4 Years ago
Sun, May 10, 20, 00:00, 4 Years ago
Sat, Jan 18, 20, 00:00, 4 Years ago
Thu, Mar 14, 19, 00:00, 5 Years ago
;