close()后没有删除RNFetchBlob blob

时间:2017-11-30 22:26:52

标签: javascript reactjs firebase react-native react-native-fetch-blob

我使用RNFetchBlob将图像从本地(ios)文件系统上传到Firebase

虽然我的图片正在被删除,但关闭后我的blob仍保留在文件系统中。 我尝试取消链接blob,但它仍然是

function createBlob(uri, mediaType) {
  const Blob = RNFetchBlob.polyfill.Blob; 
  const typePrefix = isImage(mediaType) ? 'image' : 'video';  
  window.Blob = Blob;
  window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;  

  return new Promise((resolve, reject) => {
    const uid = guid();
    const mediaRef = firebase.storage().ref('users').child(`${uid}.${mediaType}`);
    let uploadBlob = null;

    Blob.build(uri, { type: `${typePrefix}/${mediaType}` })
    .then((blob) => {
      uploadBlob = blob;
      return mediaRef.put(blob, { type: `${typePrefix}/${mediaType}` });
    })
    .then((response) => {
      uploadBlob.close();

      resolve(response);
    })
    .catch(error => {
      reject(error);
    });
  });
}

关闭blob后,它在我的模拟器文件夹中的显示方式

enter image description here

createBlob后我使用RNFetchBlob.fs.unlink(path)

从本地存储中删除文件

1 个答案:

答案 0 :(得分:2)

我认为您close()正在工作,因为uploadBlob变量是在前一个闭包中创建的,但未传递到下一个.then块。我认为这可能有用......

Blob.build(uri, { type: `${typePrefix}/${mediaType}` })
.then((blob) => {
  uploadBlob = blob;
  return ({
     response: mediaRef.put(blob, { type: `${typePrefix}/${mediaType}` }),
     blob: uploadBlob
})
.then(({response, blob}) => {
  blob.close();

  resolve(response);
})