Created
January 6, 2022 07:06
-
-
Save elipettingale/ce9174914e579af334cf61524e060198 to your computer and use it in GitHub Desktop.
Image file upload preview
This file contains hidden or 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
| export default class ImageUpload { | |
| constructor(root) { | |
| this.root = root; | |
| this.input = root.querySelector('input'); | |
| this.preview = root.querySelector('img'); | |
| this.input.addEventListener('change', (event) => this.onChange(event)); | |
| } | |
| onChange(event) { | |
| let file = event.target.files[0]; | |
| if (!file) { | |
| return; | |
| } | |
| let reader = new FileReader(); | |
| reader.readAsDataURL(file); | |
| reader.addEventListener('load', () => this.updatePreview(reader.result)); | |
| } | |
| updatePreview(url) { | |
| this.preview.style.backgroundImage = `url('${url}')`; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment