Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress not working in version 17.0.0 and higher #248

Closed
grazieleoliveira opened this issue May 17, 2023 · 4 comments
Closed

Progress not working in version 17.0.0 and higher #248

grazieleoliveira opened this issue May 17, 2023 · 4 comments

Comments

@grazieleoliveira
Copy link

Hi guys!

I've implemented a download function in my react native project using the latest version of react-native-blob-util, but the progress callback was never being called

I got to make it work out by downgrading the library version to 16.4.0, but the latest version really doesn't work. I want to be able to use the latest versions, can you help me? 🙌

@RonRadtke
Copy link
Owner

Hi, thanks for using the lib and your report.
Do you have the problems on Android or iOS?
Could you provide a small code snippet of what you're doing?

@grazieleoliveira
Copy link
Author

grazieleoliveira commented May 18, 2023

Thanks for the reply, Ron!

I've tested on iOS, didn't get to test it on android

Heres the code snippet:

import { Platform } from 'react-native';
import RNFetchBlob from 'react-native-blob-util';
import { Observable } from 'rxjs';
import { IDownloadTaskProps } from './types';
import {
  COMPLETED_DOWNLOAD_STATUS,
  COMPLETED_DOWNLOAD_PROGRESS,
  DEFAULT_CONFIG,
  DOWNLOAD_IN_PROGRESS_STATUS,
  TOTAL_PERCENT,
  DIR_NAME,
} from './constants';
import { ITaskObservable } from '@/modules/download/types';


  export const downloadTask = (data: IDownloadTaskProps): ITaskObservable => {
    const {
      fs: { dirs },
      config,
    } = RNFetchBlob;
    const rootDir = dirs[Platform.OS === 'ios' ? 'DocumentDir' : 'DownloadDir'];
    const path = `${rootDir}/${DIR_NAME}/${data?.fileName}`;
  
    return new Observable((observer) => {
      const downloadProgress = (received: number, total: number) => {
        const progress = Math.round((received / total) * TOTAL_PERCENT);
        observer.next({
          status: DOWNLOAD_IN_PROGRESS_STATUS,
          progress,
          taskId: data.taskId,
          contentData: undefined,
        });
      };
      try {
        const response = config({
          ...(data.configOptions ?? DEFAULT_CONFIG),
          path,
        }).fetch(data.fetchMethod, data.fileUrl);
  
        response
          .progress(downloadProgress)
          .then((res) => {
            observer.next({
              status: COMPLETED_DOWNLOAD_STATUS,
              progress: COMPLETED_DOWNLOAD_PROGRESS,
              taskId: data?.taskId,
              contentData: res,
            });
            observer.complete();
          })
          .catch((err) => {
            observer.error(`Error while trying to download: ${err}`);
          });
      } catch (error) {
        observer.error(`Error while trying to write file: ${error}`);
      }
    });
};

@alessandro-bottamedi
Copy link

Same issue here on iOS v 17.3

@RonRadtke
Copy link
Owner

RonRadtke commented May 22, 2023

Do you get the status and headers of the request set correctly?
It might be related to this issue: #236
So maybe you can try my feature-branch as well and see if that solves your problems with the progress. It's a reimplementation of the event handling

RonRadtke pushed a commit that referenced this issue May 31, 2023
* Additional parameter for androiddownloads to enforce the downloaded file being added to the downloads collection

resolves #247
resolves #248
resolves #245
resolves #244
resolves #236

Merge branch 'develop'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants