I'm working on a Reactjs project where I can upload and download files.
But when I upload a file, the request PUT from Axios is completed before the file is fully uploaded to my database. I use the onUploadProgress from Axios and it shows directly 100% after I click on the upload button whereas the file is not uploaded yet.
It even seems that the upload of the file on the back side is only starts once the progress bar reaches 100%. I don't know how to correctly synchronize the progress bar with the file uploading.
My code
Front
onUpload() {
const data = new FormData();
for (var i = 0; i < this.state.selectedFile.length; i++) {
data.append("file", this.state.selectedFile[i]);
}
axios
.put("http://localhost:5000/api/files/upload", data, {
onUploadProgress: ProgressEvent => {
this.setState({
loaded: Math.round(
(ProgressEvent.loaded * 100) / ProgressEvent.total
)
});
}
})
.then(res => {
console.log("File(s) uploaded", res);
});
}
Back
export const uploadFile = (req, res) => {
const myFile = (Array.isArray(req.files.file)
? req.files.file
: [req.files.file]
).filter(e => e);
// upload file(s) to directory
for (let i = 0; i < myFile.length; i++) {
myFile[i].mv("./src/uploads/" + myFile[i].name, function(err) {
if (err) {
return res.status(500).send(err);
}
});
// save file(s) to database
Upload.create({
type: myFile[i].mimetype,
name: myFile[i].name,
data: myFile[i].data
});
}
res.send("file uploaded !");
};
By the way I'm using https://www.npmjs.com/package/express-fileupload for file upload
EDIT
onUpload() {
const data = new FormData();
for (var i = 0; i < this.state.selectedFile.length; i++) {
data.append("file", this.state.selectedFile[i]);
}
axios
.post("http://localhost:5000/api/files/upload", data, {
headers: {
"Content-type": "multipart/form-data"
},
onUploadProgress: ProgressEvent => {
this.setState({
loaded: Math.round(
(ProgressEvent.loaded * 100) / ProgressEvent.total
)
});
}
})
.then(res => {
console.log("File(s) uploaded", res);
});
}
multipart/form-data(the second one is the most important) Try using this content-type header - Mehdi Benmoha