在我的应用程序中,我有一个api返回文件。但是,我不知道文件的扩展名。
如何在React-Native(0.59.8)中下载该文件?
答案 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;
}