Created
September 26, 2023 06:07
-
-
Save andrzey/f20dba0485bd95f87ce07d654ce906aa to your computer and use it in GitHub Desktop.
Upload progress axios
This file contains hidden or 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
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