我发现了一些有用的插件。
1.反应天然取入团块
https://github.com/wkh237/react-native-fetch-blob/wiki/Classes#rnfetchblobconfig
它具有IOSBackgroundTask
选项。
RNFetchBlob
.config({
path : dest_file_path,
IOSBackgroundTask: true,
overwrite: true,
indicator: true,
})
.fetch('GET', download_url, {
//some headers ..
})
.progress((received, total) => {
console.log('progress : '+ received + '/' + total);
})
.then((res) => {
console.log('# The file saved to :', file_path);
})
顺便说一下,它看起来不能正常工作。 不知道如果我错过了什么......
2.反应母语-FS
https://github.com/itinance/react-native-fs#downloadfileoptions-downloadfileoptions--jobid-number-promise-promisedownloadresult-
const ret = RNFS.downloadFile({
fromUrl: download_url,
toFile: dest_file_path,
connectionTimeout: 1000 * 10,
background: true,
discretionary: true,
progressDivider: 1,
resumable: (res) => {
console.log("# resumable :", res);
},
begin: (res) => {
// start event
},
progress: (data) => {
const percentage = ((100 * data.bytesWritten)/data.contentLength) | 0;
console.log("# percentage :", percentage);
},
});
jobId = ret.jobId;
ret.promise.then((res) => {
console.log('Download finished.');
RNFS.completeHandlerIOS(jobId);
jobId = -1;
}).catch(err => {
console.log('error');
jobId = -1;
});
它看起来运行良好。
顺便说一句,当我尝试通过推送通知在后台下载时,它不会开始下载,除非我打开该应用程序。
任何人都可以解决这个问题?
+1给RNFS示例。根据我的经验,RNFetchBlob是一个不稳定的选择,原因有两个。 1.没有维护。 2.它会在大文件下载时阻塞JS线程,这会导致你的应用“冻结”,直到这个过程结束。 –