如何在不知道React-Native扩展名的情况下从url获取文件

时间:2020-02-28 08:43:53

标签: javascript reactjs react-native fetch

在我的应用程序中,我有一个api返回文件。但是,我不知道文件的扩展名。

如何在React-Native(0.59.8)中下载该文件?

1 个答案:

答案 0 :(得分:0)

尝试使用下面给出的代码。

  import RNFetchBlob from 'react-native-fetch-blob';
  download = async(path) =>{
          console.log('URL', path);

          var date      = new Date();
          var url       =  path;
          var ext       = this.extention(path);
          ext = "."+ext[0];
          const { config, fs } = RNFetchBlob
          let PictureDir = fs.dirs.PictureDir
          let options = {
            fileCache: true,
            addAndroidDownloads : {
              useDownloadManager : true,
              notification : true,
              path:  PictureDir + "/MyFile_"+Math.floor(date.getTime() + date.getSeconds() / 2)+ext,
              description : 'File'
            }
          }
          config(options).fetch('GET', url).then((res) => {
         Alert.alert("Success Downloaded");

    });
        }
      extention(filename){
          return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
        }