Skip to content Skip to sidebar Skip to footer

Javascript Save Blob To Localstorage

I am trying to save blob data (favicon) retrieved via AJAX, to localStorage. Code : var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://g.etfv.co/http://www.google.com', true)

Solution 1:

Just store the blob as a data uri in local storage

var xhr = new XMLHttpRequest();
xhr.open('GET', 
'http://g.etfv.co/http://www.google.com',
true);
xhr.responseType = "blob";
xhr.onload = function(e){ //Stringify blob...
    //reload the icon from storage
    var fr = new FileReader();
    fr.onload = 
        function(e) {
            localStorage['icon'] = e.target.result;
            document.getElementById("myicon").src = localStorage['icon'];
        }
    fr.readAsDataURL(xhr.response);
}
xhr.send(null);

Post a Comment for "Javascript Save Blob To Localstorage"