Skip to content

Instantly share code, notes, and snippets.

@cdmz
Created January 4, 2016 12:28
Show Gist options
  • Save cdmz/4393cf7e13ddc18a848e to your computer and use it in GitHub Desktop.
Save cdmz/4393cf7e13ddc18a848e to your computer and use it in GitHub Desktop.
base64 files with javascript
var handleFileSelect = function(evt) {
var files = evt.target.files;
var file = files[0];
if (files && file) {
var reader = new FileReader();
reader.onload = function(readerEvt) {
var binaryString = readerEvt.target.result;
console.log(btoa(binaryString));
};
reader.readAsBinaryString(file);
}
};
if (window.File && window.FileReader && window.FileList && window.Blob) {
document.getElementById('inputFile').addEventListener('change', handleFileSelect, false);
} else {
alert('The File APIs are not fully supported in this browser.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment