如何将多个文件或图像发送到后端。 我正在使用React js作为前端部分。
答案 0 :(得分:0)
let files = // list of files
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
formdata.append(`images[${i}]`, {
uri: pathOfFile,
name: fileName.jpg,
type: mimeType(eg. "image/jpeg")
});
}
//if you are using axios
axios.post('https://mywebsite.com/endpoint/', formData, {
headers: {
'content-type': 'multipart/form-data'
}
}).then(response => {
console.log(response.data)
});
//if you are using fetch
fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: formData,
});