使用React Native Fetch Blob下载图像文件

时间:2018-09-06 21:58:53

标签: react-native

我正在尝试学习本机响应以及如何将文件下载到设备。我知道您可以通过React Native文件系统来做到这一点,但这不支持我需要的身份验证。 React native提取blob确实支持这一点。

出于教育目的,我希望在react native fetch blob(而不是react native文件系统)中重新创建该问题的第一个答案的代码。

我可以给我写一个例子,我会超级无聊。

问题:Downloading data files with React Native for offline use

2 个答案:

答案 0 :(得分:1)

尝试一下,在Android上可以正常使用

export const download = (url, name) => {
  const { config, fs } = RNFetchBlob;
  let PictureDir = fs.dirs.PictureDir;
  let options = {
    fileCache: true,
    addAndroidDownloads: {
      useDownloadManager: true,
      notification: true,
      path: PictureDir + name,
      description: t('downloading_file')
    }
  };
  config(options)
    .fetch('GET', url)
    .then(res => {
      if (res.data) {
        alert(t('download_success'));
      } else {
        alert(t('download_failed'));
      }
    });
};

答案 1 :(得分:0)

downloadImage(){
   var date      = new Date();
   var url       = "http://debasish.com/image.jpg";
   var ext       = this.getExtention(url);
   ext = "."+ext[0];
   const { config, fs } = RNFetchBlob ; 
   let PictureDir = fs.dirs.PictureDir
   let options = {
   fileCache: true,
    addAndroidDownloads : {
      useDownloadManager : true,
      notification : true,
      path:  PictureDir + "/image_"+Math.floor(date.getTime() 
          + date.getSeconds() / 2)+ext,
     description : 'Image'
      }
   }
    config(options).fetch('GET', url).then((res) => {
      Alert.alert("Download Success !");
   });
}
  getExtention(filename){
    return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : 
undefined;

}

相关问题