通过React Native Fetch Blob删除下载的图像



我正在使用rnfetchblob作为base64格式共享图像。但是图像正在我的本地文件夹中下载。我想删除它。根据我的研究知识,我想取消链接图像路径。我不知道该怎么做。这是我的代码

_downloadImageAndShare(url ,title, message) {
const { fs } = RNFetchBlob.fs;
const self = this;
RNFetchBlob.config({ fileCache: true })
  .fetch('GET', url)
  .then(resp => resp.readFile('base64')
      .then(base64 => ({ resp, base64 })))
  .then(obj => {
    const headers = obj.resp.respInfo.headers;
    const type = headers['Content-Type'];
    const dataUrl = 'data:' + type + ';base64,' + obj.base64;
    return { url: dataUrl, title, message };
  })
  .then(options => Share.open(options)).catch(err => {err && 
  console.log(err); })         
}

如何在此方法中使用此代码

RNFetchBlob.fs.unlink(path)
.then(() => { ... })
.catch((err) => { ... })

以及如何指定UNLINK(路径(??

预先感谢。

您可以使用config使用path参数指定文件位置。

const filePath = RNFetchBlob.fs.dirs.DocumentDir + '/myFile';
RNFetchBlob.config({ fileCache: true, path : filePath })
  .fetch('GET', url)
  .then(resp => resp.readFile('base64')
      .then(base64 => ({ resp, base64 })))
  .then(obj => {
    const headers = obj.resp.respInfo.headers;
    const type = headers['Content-Type'];
    const dataUrl = 'data:' + type + ';base64,' + obj.base64;
    return { url: dataUrl, title, message };
  })
  .then(options => Share.open(options)).catch(err => {err && 
  console.log(err); })         
}

然后您可以像这个

一样调用unlink
RNFetchBlob.fs.unlink(filePath)
.then(() => { ... })
.catch((err) => { ... })

最新更新