Last active
March 10, 2022 22:31
-
-
Save varemenos/fda46839c9adf32c7920 to your computer and use it in GitHub Desktop.
Get an asset via the Fetch API and convert it to a base64 string
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
var path = 'http://adonisk.com/img/vlogo.jpg'; | |
fetch(path).then(function (response) { | |
response.body.getReader().read().then(function(result) { | |
return btoa(String.fromCharCode.apply(null, result.value)); | |
}).then(function(b64) { | |
console.log(b64); | |
}); | |
}); |
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
var fetcher = function (path) { | |
return new Promise(function (resolve) { | |
fetch(path).then(function (response) { | |
response.body.getReader().read().then(function(result) { | |
return btoa(String.fromCharCode.apply(null, result.value)); | |
}).then(function(result) { | |
resolve(result); | |
}); | |
}); | |
}); | |
}; | |
Promise.all([ | |
fetcher('//cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js'), | |
fetcher('//cdnjs.cloudflare.com/ajax/libs/Base64/0.3.0/base64.min.js') | |
]).then(function (results) { | |
console.log(results); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment