Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Last active January 18, 2021 21:23
Show Gist options
  • Save SethVandebrooke/f45ddd5a90014e5f0a2c6a57cb62e052 to your computer and use it in GitHub Desktop.
Save SethVandebrooke/f45ddd5a90014e5f0a2c6a57cb62e052 to your computer and use it in GitHub Desktop.
Upload and file and convert it into a base64 data url
function uploadAndConvertToBase64DataURL(callback) {
if (!document.getElementById("B64DURL_UL")) {
var input = document.createElement("input");
input.id = "B64DURL_UL";
input.style.display = "none";
input.type = "file";
document.body.appendChild(input);
}
var input = document.getElementById("B64DURL_UL");
input.addEventListener("change", function (e){
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = function (e) {
callback(e.target.result);
};
reader.readAsDataURL(file);
}, false);
input.click();
}
// uploadAndConvertToBase64DataURL(function(dataURL){...})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment