Last active
April 9, 2020 14:07
-
-
Save ogiovannyoliveira/59869bc65591c308160b5d5f89e347eb to your computer and use it in GitHub Desktop.
File example for preview of images before 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> | |
<input ref="inputFile" type="file" accept="image/*" hidden @change="upload"> | |
<button @click="openInputFile">Upload</button> | |
<div> | |
<div v-show="view"> | |
<img id="imgFile" style="width: 600px; height: 400px" /> | |
<button @click="cleanInputFile"><i>×</i></button> | |
</div> | |
</div> | |
</div> | |
</template> | |
<script> | |
export default { | |
data: () => ({ | |
view: false | |
}), | |
methods: { | |
openInputFile() { | |
this.$refs.inputFile.click(); | |
}, | |
upload(event) { | |
const file = event.target.files[0]; | |
const preview = document.getElementById("imgFile"); | |
const fr = new FileReader(); | |
fr.onload = e => preview.src = e.target.result; | |
fr.readAsDataURL(file) | |
this.view = true; | |
}, | |
cleanInputFile() { | |
this.view = false; | |
const preview = document.getElementById("imgFile"); | |
const fr = new FileReader(); | |
fr.onload = () => preview.src = ""; | |
}, | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment