удалить загруженное изображение, отреагировав на собственный объект 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 (путь) ??

заранее спасибо.


person khalifathul    schedule 02.04.2018    source источник


Ответы (1)


Вы можете указать местоположение файла с помощью 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) => { ... })
person Harikrishnan    schedule 02.04.2018
comment
что насчет того + '/ myFile' будет? @Harikrishnan - person khalifathul; 02.04.2018
comment
но я не знаю имени файла ... это URL-адрес, поступающий из службы - person khalifathul; 02.04.2018
comment
Вы не обязаны. Вы можете сохранить в этом имени файла. - person Harikrishnan; 03.04.2018
comment
просто я оставлю это как '/ myFile'? - person khalifathul; 03.04.2018