简体   繁体   English

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

[英]How to fetch file from url without knowing its extension in React-Native

In my app I have an api that returns back a file.在我的应用程序中,我有一个返回文件的 api。 However, I don't know the extension of the file in advance.但是,我事先不知道文件的扩展名。

How can I download that file in React-Native(0.59.8)?如何在 React-Native(0.59.8) 中下载该文件?

Try using the below given code.尝试使用下面给出的代码。

  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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM