Download Progress
I facing problem when keep tracking download progress, this is a question posted in stackoverflow
Not every time console.log in _downloadFileProgress show Progress 100%, so it is harder for me to check the progress. Did I miss out some setting, or there is any other ways to keep track the progress correctly. Mean every time download is completed, percentage show 100%
RNFS.downloadFile(config) returns jobId and promise, then is called when the download is finished.
Example:
const task = RNFS.downloadFile(config);
task.promise.then(() => {
// download successfully finished
});
Hi I implement using this by following RNFS npm guidelines https://www.npmjs.com/package/react-native-fs For progress bar I used library React Native Progress Circle downloadFile(options: DownloadFileOptions): { jobId: number, promise: Promise<DownloadResult> }
type DownloadFileOptions = {
fromUrl: string; // URL to download file from
toFile: string; // Local filesystem path to save the file to
headers?: Headers; // An object of headers to be passed to the server
background?: boolean; // Continue the download in the background after the app terminates (iOS only)
discretionary?: boolean; // Allow the OS to control the timing and speed of the download to improve perceived performance (iOS only)
cacheable?: boolean; // Whether the download can be stored in the shared NSURLCache (iOS only, defaults to true)
progressDivider?: number;
begin?: (res: DownloadBeginCallbackResult) => void;
progress?: (res: DownloadProgressCallbackResult) => void;
resumable?: () => void; // only supported on iOS yet
connectionTimeout?: number // only supported on Android yet
readTimeout?: number // supported on Android and iOS
};
Here I implement above lines of code like this
RNFS.downloadFile({
fromUrl: encodedfileURL,
toFile: downloadfilePath,
//headers
background: true, **// Continue the download in the background after the app terminates (iOS only)**
discretionary: true, **// Allow the OS to control the timing and speed of the download to improve perceived performance (iOS only)**
cacheable: true, **// Whether the download can be stored in the shared NSURLCache (iOS only, defaults to true)**
begin: (res: DownloadBeginCallbackResult) => {
console.log("Response begin ===\n\n");
console.log(res);
},
progress: (res: DownloadProgressCallbackResult) => {
//here you can calculate your progress for file download
console.log("Response written ===\n\n");
let progressPercent = (res.bytesWritten / res.contentLength)*100; // to calculate in percentage
console.log("\n\nprogress===",progressPercent)
this.setState({ progress: progressPercent.toString() });
item.downloadProgress = progressPercent;
console.log(res);
}
})
.promise.then(res => {
console.log("res for saving file===", res);
return RNFS.readFile(downloadfilePath, "base64");
})
I don't know what's wrong and I know both expressions are the same mathematically but
this one didn't give me correct progress (let progressPercent = (res.bytesWritten / res.contentLength)*100; // to calculate in percentage)
but this one gave me the correct progress (let progressPercent = ((100 * res.bytesWritten) / res.contentLength))
It doesn't make any sense to me