Skip to content

Instantly share code, notes, and snippets.

@andrit
Created July 25, 2018 15:19
Show Gist options
  • Save andrit/71e5cb0a9c156be01756ad896114847b to your computer and use it in GitHub Desktop.
Save andrit/71e5cb0a9c156be01756ad896114847b to your computer and use it in GitHub Desktop.
function getBase64(file) {
   var reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = function () {
     console.log(reader.result);
   };
   reader.onerror = function (error) {
     console.log('Error: ', error);
   };
}

var file = document.querySelector('#files > input[type="file"]').files[0];
getBase64(file); // prints the base64 string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment