首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在localforage中存储图像(例如头像)?

如何在localforage中存储图像(例如头像)?
EN

Stack Overflow用户
提问于 2019-05-07 19:12:06
回答 1查看 91关注 0票数 0

我有一个在我的网站上编辑个人资料的功能,其中我必须更改个人资料图片。它会发生变化,但在页面刷新时,会恢复到旧的个人资料图片。为了解决这个问题,我尝试将它存储在localforage中。问题是,它不会存储在localforage中,并且在刷新时,图片会恢复为旧图片。如何在localforage中正确地存储图像(或其URL,视情况而定)?

我试着这样存储它:

代码语言:javascript
复制
const { profilePic } = this.props;
localforage.getItem('currentUser').then((currentUser) => {
  currentUser.profile_pic.url = profilePic;
  localforage.setItem('currentUser',currentUser);
});

其中currentUser如下所示:

我希望新上传的图像保存在localforage中。现在,它没有被正确地存储在localforage中。

EN

回答 1

Stack Overflow用户

发布于 2019-05-07 20:14:07

请尝试

代码语言:javascript
复制
const { profilePic } = this.props;
localforage.getItem('currentUser').then((currentUser) => {
   currentUser.profile_pic.url = profilePic;
   localforage.setItem('currentUser',currentUser).then(()=>{});
});

我试过了

代码语言:javascript
复制
localforage.getItem('somekey').then((value) => {
  // This code runs once the value has been loaded
  // from the offline store.
  localforage.setItem('somekey', 'some other value').then(()=>{

})

}).catch(function(err) {
  // This code runs if there were any errors
  console.log(err);
});
localforage.getItem('somekey').then(function(value) {
  // This code runs once the value has been loaded
  // from the offline store.
  console.log(value);
}).catch(function(err) {
  // This code runs if there were any errors
  console.log(err);
});

这对我很有效。:-)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56021316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档