Skip to content

Instantly share code, notes, and snippets.

@stockenberg
Created April 9, 2019 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stockenberg/41a3d8e9aa3d24e2c5367da9ec619125 to your computer and use it in GitHub Desktop.
Save stockenberg/41a3d8e9aa3d24e2c5367da9ec619125 to your computer and use it in GitHub Desktop.
JS Upload + Axios and Progress
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="">
<input type="file" name="image" id="image" onchange="changeImage()">
</form>
<script src="node_modules/axios/dist/axios.min.js"></script>
<script>
var config = {
onUploadProgress: function(progressEvent) {
var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
console.log(percentCompleted);
},
headers: {
'Content-Type': 'multipart/form-data'
}
};
function changeImage(){
let image = document.forms[0][0].files[0];
let formData = new FormData();
formData.append('image', image);
axios.post('http://localhost:8000/upload.php', formData, config).then(res => {
console.log(res);
})
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment