Created
February 13, 2021 18:24
-
-
Save TiagoSilvaPereira/24206c2b7077a2635649987c4efd6f9b to your computer and use it in GitHub Desktop.
Alpine.js image viewer with fileChosen method
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
function imageViewer() { | |
return { | |
imageUrl: '', | |
fileChosen(event) { | |
this.fileToDataUrl(event, src => this.imageUrl = src) | |
}, | |
fileToDataUrl(event, callback) { | |
if (! event.target.files.length) return | |
let file = event.target.files[0], | |
reader = new FileReader() | |
reader.readAsDataURL(file) | |
reader.onload = e => callback(e.target.result) | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment