Skip to content

Instantly share code, notes, and snippets.

@praveen-vishnu
Created February 3, 2020 16:42
Show Gist options
  • Save praveen-vishnu/d1dddb836a3317d0180acf1d5a983ccb to your computer and use it in GitHub Desktop.
Save praveen-vishnu/d1dddb836a3317d0180acf1d5a983ccb to your computer and use it in GitHub Desktop.
Convert an image url to a base64 data URL
this.toDataURL(this.data.photoUrl, (dataUrl) => {
console.log(dataUrl);
})
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment