Skip to content

Instantly share code, notes, and snippets.

@moshiurse
Created March 16, 2020 05:55
Show Gist options
  • Save moshiurse/877d12b4d53de264a886bd059719a2e4 to your computer and use it in GitHub Desktop.
Save moshiurse/877d12b4d53de264a886bd059719a2e4 to your computer and use it in GitHub Desktop.
Convert any file to base64 in JS
$(document).ready(function (){
function getBase64File (file){
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
return reader.result;
}
$('#files').on('change', function (e) {
var file = e.target.files[0];
// console.log(file);
var srcFile = getBase64File(file);
console.log(srcFile);
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment