Skip to content

Instantly share code, notes, and snippets.

@andrzey
Created September 26, 2023 06:07
Show Gist options
  • Save andrzey/f20dba0485bd95f87ce07d654ce906aa to your computer and use it in GitHub Desktop.
Save andrzey/f20dba0485bd95f87ce07d654ce906aa to your computer and use it in GitHub Desktop.
Upload progress axios
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');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment