例如,假设用户将一些非常大的图像或媒体文件加载到您的Web应用程序中。当它们返回时,您希望您的应用程序显示它们先前已加载的内容,但由于数据太大而无法将实际文件数据保留在LocalStorage中。
最佳答案
localStorage
不能使用,而不能使用。存储在localStorage
中的数据必须是可以序列化的原始类型之一。这不包括File
对象。
例如,这将而不是正常工作:
var el = document.createElement('input');
el.type='file';
el.onchange = function(e) {
localStorage.file = JSON.stringify(this.files[0]);
// LATER ON...
var reader = new FileReader();
reader.onload = function(e) {
var result = this.result; // never reaches here.
};
reader.readAsText(JSON.parse(localStorage.f));
};
document.body.appendChild(el);
解决方案是使用更强大的存储选项,例如将文件内容写入HTML5 Filesystem或将其存储在IndexedDB中。
关于javascript - 当用户返回页面时,是否可以将File对象保存在LocalStorage中,然后通过FileReader重新加载文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6158829/