Skip to content

Instantly share code, notes, and snippets.

@psbolden
Created April 14, 2017 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save psbolden/fdecf4ba95ba01a95c2ab20c799e30d8 to your computer and use it in GitHub Desktop.
Save psbolden/fdecf4ba95ba01a95c2ab20c799e30d8 to your computer and use it in GitHub Desktop.
Image to DataUrl using File Reader
//Source: http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript
$( "#imagetourl" ).click(function() {
function toDataUrl(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
toDataUrl('https://source.unsplash.com/bWI4Vd4vI3w/500x500', function(base64Img) {
//console.log(base64Img);
window.open(base64Img)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment