Created
March 16, 2020 05:55
-
-
Save moshiurse/877d12b4d53de264a886bd059719a2e4 to your computer and use it in GitHub Desktop.
Convert any file to base64 in JS
This file contains 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
$(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