Skip to content

Instantly share code, notes, and snippets.

@varemenos
Last active March 10, 2022 22:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save varemenos/fda46839c9adf32c7920 to your computer and use it in GitHub Desktop.
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
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);
});
});
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