varemenos
9/5/2015 - 3:41 PM

Get an asset via the Fetch API and convert it to a base64 string

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);
});