Created
July 4, 2020 17:06
-
-
Save lanks/e175366c4cb88f7e7a7999eb78107df2 to your computer and use it in GitHub Desktop.
Read an image to base64 then show it in an image element
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
| static targets = ["image"] | |
| function readImageToBase64(input, callback) { | |
| if (input.files && input.files[0]) { | |
| var reader = new FileReader(); | |
| reader.onload = function (e) { | |
| callback(e.target.result); | |
| }; | |
| reader.readAsDataURL(input.files[0]); | |
| } | |
| } | |
| imageSelected(e){ | |
| readImageToBase64(e.currentTarget, function(image_url) { | |
| imageTarget.src = image_url | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment