Skip to content

Instantly share code, notes, and snippets.

@chrisvasey
Created June 19, 2019 15:43
Show Gist options
  • Save chrisvasey/2d06ffca46e7b0c7939535bde04f0cb0 to your computer and use it in GitHub Desktop.
Save chrisvasey/2d06ffca46e7b0c7939535bde04f0cb0 to your computer and use it in GitHub Desktop.
File upload in Vue
<template>
<input type="file" @change="processFile($event)">
</template>
<script>
export default {
data(){
return {
file: null,
}
},
methods: {
processFile(event) {
this.form = event.target.files[0];
this.save();
},
save(){
var self = this;
var formData = new FormData();
formData.append('file', this.file);
axios.post('/test',
formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment