【发布时间】:2019-02-12 05:21:53
【问题描述】:
我正在尝试学习本机反应以及如何将文件下载到设备。我知道您可以使用本机文件系统来执行此操作,但这不支持我需要的身份验证。 React 本机 fetch blob 确实支持这一点。
出于教育目的,我希望在 react native fetch blob 而不是 react native 文件系统中重新创建此问题的第一个答案中的代码。
我有人可以写一个例子,我会超级坦克。
【问题讨论】:
标签: react-native
我正在尝试学习本机反应以及如何将文件下载到设备。我知道您可以使用本机文件系统来执行此操作,但这不支持我需要的身份验证。 React 本机 fetch blob 确实支持这一点。
出于教育目的,我希望在 react native fetch blob 而不是 react native 文件系统中重新创建此问题的第一个答案中的代码。
我有人可以写一个例子,我会超级坦克。
【问题讨论】:
标签: react-native
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;
}
【讨论】:
试试这个,它在 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'));
}
});
};
【讨论】: