【问题标题】:How to store an image (e.g. profile picture) in localforage?如何在 localforage 中存储图像(例如个人资料图片)?
【发布时间】:2019-05-07 20:14:06
【问题描述】:

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

我试过这样存储它:

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

其中 currentUser 如下所示:

我希望将新上传的图片保存在 localforage 中。现在它没有正确存储在 localforage 中。

【问题讨论】:

    标签: reactjs localforage


    【解决方案1】:

    请尝试

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

    这对我有用。 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2014-09-18
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多