Last active
October 10, 2024 15:11
-
-
Save virolea/e1af9359fe071f24de3da3500ff0f429 to your computer and use it in GitHub Desktop.
Tracking file upload progress using axios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upload(files) { | |
const config = { | |
onUploadProgress: function(progressEvent) { | |
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total) | |
console.log(percentCompleted) | |
} | |
} | |
let data = new FormData() | |
data.append('file', files[0]) | |
axios.put('/endpoint/url', data, config) | |
.then(res => console.log(res)) | |
.catch(err => console.log(err)) | |
} |
Thanks
Thank you ♥
Very useful,have a wonderful day!
Thanks you 🚀
Thanks a bunch! Clean & nice code
Спасибо!
With some improvements:
const onUploadProgress = event => {
const percentCompleted = Math.round((event.loaded * 100) / event.total);
console.log('onUploadProgress', percentCompleted);
};
const upload = async files => {
const data = new FormData();
for(const [index, file] of files.entries()){
data.append(index, file); // append all files
}
try {
const result = await axios.put('/endpoint/url', data, {onUploadProgress});
console.log('result is', result); // result is server's response
} catch(error){
console.error(error);
} finally {
console.log('Upload complete');
}
}
Very thanks all to be so kind to share a great code!!
Verry nice! Thanks
Thanks a lot!!
Thanks!
Useful, thanks!
I when try take request.upload.addEventListener in not a function
thanks bro
wow, this code works exactly as written by you. thanks man, nice code
Thank you!
This is code and I thank.
I am not a bot.
simple, efficient & works... thanks a lot
Thanks a lot!
Thanks.
you lou bro :)
Really helpful, thanks
Nice. thanks!
Thank you bro
Hi, but this is giving me 401 unauthorized error when i call my API, Why is that?
Thank :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks