js File类型本地存储与读取
首先将File文件转为base64利用FileReader的读取功能,完成后会触发onload事件,result属性即为base64编码由于得到的base64编码基本会超过5M,所以需要对localStorage进行额外操作,可以用localforage:优先使用IndexedDB存储,如果不支持,使用WebSQL,浏览器还不支持,使用localStorage下载:https://github.c
![](https://csdnimg.cn/release/devpress/public/img/ic-book.4f347164.png)
一键AI生成摘要,助你高效阅读
问答
·
首先将File文件转为base64
利用FileReader的读取功能,完成后会触发onload事件,result属性即为base64编码
由于得到的base64编码基本会超过5M,所以需要对localStorage进行额外操作,
可以用localforage:优先使用IndexedDB
存储,如果不支持,使用WebSQL
,浏览器还不支持,使用localStorage
下载:https://github.com/localForage/localForage/releases
// file为File类型文件
var reader = new FileReader();
reader.onload = () => {
localforage.setItem(file.name, reader.result);
};
reader.readAsDataURL(file);
最后读取存储的数据
// fileName为存储的key
localforage.getItem(fileName, (err, value) => {
if (err === null && value) {
// 得到value
}
});
更多推荐
所有评论(0)