Skip to content

Instantly share code, notes, and snippets.

@lanks
Created July 4, 2020 17:06
Show Gist options
  • Select an option

  • Save lanks/e175366c4cb88f7e7a7999eb78107df2 to your computer and use it in GitHub Desktop.

Select an option

Save lanks/e175366c4cb88f7e7a7999eb78107df2 to your computer and use it in GitHub Desktop.
Read an image to base64 then show it in an image element
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