Forked from psbolden/gist:fdecf4ba95ba01a95c2ab20c799e30d8
Created
May 6, 2019 18:01
-
-
Save YEK-PLUS/b479f0ba046707cc2b483656d8e20e1e to your computer and use it in GitHub Desktop.
Image to DataUrl using File Reader
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
//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