如何在localforage中存储图片(例如个人资料图片)?

时间:2019-05-07 11:12:06

标签: reactjs localforage

我在网站上具有编辑个人资料的功能,必须在其中更改个人资料图片。它已更改,但在页面刷新时,恢复为旧的个人资料图片。为了解决此问题,我试图将其存储在localforage中。问题是,它没有存储在localforage中,并且在刷新时,图片恢复为旧的。 如何在本地存储中正确存储图像(或其URL,以适当的为准)?

我尝试过这样存储它:

const { profilePic } = this.props;
localforage.getItem('currentUser').then((currentUser) => {
  currentUser.profile_pic.url = profilePic;
  localforage.setItem('currentUser',currentUser);
});

其中的currentUser如下所示:

enter image description here

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

1 个答案:

答案 0 :(得分:0)

请尝试

const { profilePic } = this.props;
localforage.getItem('currentUser').then((currentUser) => {
   currentUser.profile_pic.url = profilePic;
   localforage.setItem('currentUser',currentUser).then(()=>{});
});

我尝试过

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);
});

这对我有用。 :-)