Created
December 29, 2018 14:32
-
-
Save crobinson42/0043b1c04714157b4d4edd8c4846905a to your computer and use it in GitHub Desktop.
Axios file upload progress indicator
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
<html> | |
<title>Axios Progress Upload</title> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script> | |
<input id="file" type="file" name="files" multiple="multiple" /><br /> | |
<br> | |
<progress id="progress" max="100" value="0"></progress> | |
<button onclick="upload();" type="button">upload w/ axios</button> | |
<script> | |
function upload() { | |
const formData = new FormData() | |
const file = document.querySelector('#file').files[0] | |
const progress = document.querySelector('#progress') | |
formData.append('files', file) | |
axios.post('//localhost:2995/upload/file', formData, { | |
onUploadProgress: progressEvent => { | |
const percent = Math.ceil((progressEvent.loaded / file.size) * 100) | |
console.log('percent', percent) | |
progress.setAttribute('value', percent) | |
}, | |
}) | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment