I have a React component in which a user can upload an unlimited number of images, which are collected in an array.
I am currently using FormData to upload these images, along with some other text fields.
However I want to get away from using FormData. Is there a better way to upload data like an array of images with plain Axios?
Component's State After 3 Image Uploads
this.state.files = [
// image file 0,
// image file 1,
// image file 2
];
Current Axios Function - Using FormData
let formData = new FormData();
this.state.files.forEach(file => formData.append('files[]',file));
let headers = { 'Content-Type': "multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2) };
axios.post('/api/upload-images',formData,{headers: headers});
Desired Axios Function - No FormData
let headers = { 'Content-Type': "multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2) };
axios.post('/api/upload-images',{...this.state},{headers: headers});
When I try to do it in the desired way, the files on received by the server are empty.