Created
May 16, 2019 23:03
-
-
Save farshidrezaei/3348070dd27fed6945800340aa3bb7ae to your computer and use it in GitHub Desktop.
VueJS simple upload
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
<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