首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跟踪react-native-fs下载进度

跟踪react-native-fs下载进度
EN

Stack Overflow用户
提问于 2017-12-27 11:41:33
回答 3查看 4.9K关注 0票数 1

使用react-native-fs从url下载文件,当下载进度百分比达到100%时,我会调用另一个函数。但是,RNFS.downloadFile没有正确跟踪进度。

代码语言:javascript
复制
_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
}

并不是每次_downloadFileProgress中的console.log都显示Progress 100%,所以我很难检查进度。我是否遗漏了一些设置,或者有任何其他方法来跟踪进度

EN

回答 3

Stack Overflow用户

发布于 2019-04-17 17:37:42

您好,我遵循RNFS npm指南实现了这一点

https://www.npmjs.com/package/react-native-fs

对于进度条,我使用了库

React Native Progress Circle

Link for below line of code

代码语言:javascript
复制
downloadFile(options: DownloadFileOptions): { jobId: number, promise: Promise<DownloadResult> }

代码语言:javascript
复制
   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
};

在这里我实现了上面的几行代码,像这样的

代码语言:javascript
复制
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");
})

票数 3
EN

Stack Overflow用户

发布于 2017-12-29 14:58:41

RNFS在下载完成时提供回调,请尝试以下操作:

代码语言:javascript
复制
RNFS.downloadFile(DownloadFileOptions).promise.then((r) => {
               //call another function here
  });
票数 2
EN

Stack Overflow用户

发布于 2018-08-24 04:12:29

您需要将其作为DownloadFileOptions中的进度选项进行传递:

代码语言:javascript
复制
progress: (data) => this._downloadFileProgress(data)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47986065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档