Created
October 20, 2017 13:32
-
-
Save juanparati/743c6f1d20c137417d6ffc150f82c6bf to your computer and use it in GitHub Desktop.
Vue 2.x Simple Upload component
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
// 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