Skip to content

Instantly share code, notes, and snippets.

@juanparati
Created October 20, 2017 13:32
Show Gist options
  • Save juanparati/743c6f1d20c137417d6ffc150f82c6bf to your computer and use it in GitHub Desktop.
Save juanparati/743c6f1d20c137417d6ffc150f82c6bf to your computer and use it in GitHub Desktop.
Vue 2.x Simple Upload component
// Simple upload from http://laravel-tricks.com/tricks/vuejs-simple-upload
<template>
<div>
<small>Change/Upload</small>
<input type="file" @change="onFileChange">
<button class="btn btn-success btn-xs" @click="upload">Upload</button>
</div>
</template>
<script>
export default {
props: ['any_props'],
data() {
return {
image: '',
file: null
}
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length)
return;
this.createImage(files[0]);
},
createImage(file) {
let reader = new FileReader();
let vm = this;
reader.onload = (e) => {
vm.image = e.target.result;
};
reader.readAsDataURL(file);
},
upload(){
axios.post('/path/to/url', {image: this.image}).then(response => {
alert('Uploaded');
});
}
}
}
</script>
<style>
/* any styles */
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment