我正在尝试将通过 AJAX 检索的 blob
数据(网站图标)保存到 localStorage
中。
代码:
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...
localStorage['icon'] = JSON.stringify(xhr.response);
//reload the icon from storage
var fr = new FileReader();
fr.onload =
function(e) {
document.getElementById("myicon").src = fr.result;
}
fr.readAsDataURL(JSON.parse(localStorage['icon']));
}
xhr.send(null);
这段代码是从这里改编而来的,在进行了一些小修改后,使其能够与localStorage
一起使用。
localStorage
将所有数据保存为字符串,因此需要在保存之前对blob进行字符串化。
JSON
不支持将blob作为其支持的类型之一,因此这段代码失败了也就不足为奇了。
有没有办法将blob存入localStorage中呢?