使用react-native-fs从 url 下载文件,当下载进度达到 100% 时,我会调用另一个函数。但是,RNFS.downloadFile
不能正确跟踪进度。
_downloadFile = () =>{
const downloadDest = `${RNFS.ExternalDirectoryPath}/Monstro/${((Math.random() * 1000) | 0)}.jpg`;
let DownloadFileOptions = {
fromUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Boothferry_Road%2C_Goole.jpg/220px-Boothferry_Road%2C_Goole.jpg",
toFile: downloadDest,
begin: this._downloadFileBegin,
progress: this._downloadFileProgress,
background:false,
progressDivider:1
};
RNFS.downloadFile(DownloadFileOptions);
}
_downloadFileBegin = () =>{
console.log("Download Begin");
}
_downloadFileProgress = (data) =>{
const percentage = ((100 * data.bytesWritten) / data.contentLength) | 0;
const text = `Progress ${percentage}%`;
console.log(text);
if(percentage == 100) //call another function here
}
不是每次都console.log
在_downloadFileProgress
演出Progress 100%
,所以我很难检查进度。我是否错过了某些设置,或者有任何其他方法可以跟踪进度