Skip to content

Instantly share code, notes, and snippets.

@virolea
Last active March 15, 2024 13:45
Show Gist options
  • Save virolea/e1af9359fe071f24de3da3500ff0f429 to your computer and use it in GitHub Desktop.
Save virolea/e1af9359fe071f24de3da3500ff0f429 to your computer and use it in GitHub Desktop.
Tracking file upload progress using axios
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))
}
@abelmiri
Copy link

Nice, is it bad to do a setState with the log?

@lchelek
Copy link

lchelek commented Sep 28, 2020

thx , usefull

@koko37
Copy link

koko37 commented Sep 30, 2020

I have added onDownloadProgress and onUploadProgress, with Rails and VueAxios.
onDownloadProgress is calling normaly, but onUploadProgress is not calling anyway.
I can not find the reason for this one.
Any suggestion for this ?

@PatelDhrupal
Copy link

You are champ.

@ShrikantaMazumder
Copy link

Thanks

@lucaswinningham
Copy link

Thanks

@Lakmal98
Copy link

Thank you ♥

@Nikolov0080
Copy link

Very useful,have a wonderful day!

@kayotimoteo
Copy link

Thanks you 🚀

@Lesleytech
Copy link

Lesleytech commented Nov 21, 2020

Thanks a bunch! Clean & nice code

@futuster
Copy link

Спасибо!

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

@IsaacMujica
Copy link

Very thanks all to be so kind to share a great code!!

@jovandyaz
Copy link

@virolea, that's still works and it's simply cool! Thanks a lot!
@futuster, nice touch!

@thanet-s
Copy link

thanet-s commented Feb 3, 2021

Verry nice! Thanks

@yarivShamash
Copy link

Thanks a lot!!

@kogrenich
Copy link

Thanks!

@rafgk
Copy link

rafgk commented Apr 22, 2021

Useful, thanks!

@paramula
Copy link

I when try take request.upload.addEventListener in not a function

@goonursultan
Copy link

thanks bro

@Hoodgail
Copy link

wow, this code works exactly as written by you. thanks man, nice code

@flash548
Copy link

Thank you!

@designbyadrian
Copy link

This is code and I thank.

I am not a bot.

@DawnImpulse
Copy link

simple, efficient & works... thanks a lot

@ImAARIX
Copy link

ImAARIX commented Jan 10, 2022

Thanks a lot!

@gabrielbarceloscn
Copy link

Thanks.

@mixelburg
Copy link

you lou bro :)

@premrajah
Copy link

Really helpful, thanks

@Roey7
Copy link

Roey7 commented May 15, 2023

Nice. thanks!

@nvtienlg
Copy link

Thank you bro

@mtalha218
Copy link

Hi, but this is giving me 401 unauthorized error when i call my API, Why is that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment