joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.84k stars 782 forks source link

Android Download Manager says path invalid but promise is neither resolved nor completed, Stuck in Queued state #287

Open teamtechalpha opened 5 years ago

teamtechalpha commented 5 years ago

Hi ! Thank you for reporting an issue, but we would like to remind you, we have a trouble shooting page in our wiki. You may want to take a look on that page or find issues tagged "trouble shooting" :p

let dir = Platform.OS === "android" ? RNFS.ExternalStorageDirectoryPath : RNFetchBlob.fs.dirs.DocumentDir; let path = dir +${DOWNLOAD_FOLDER}/download.zip`;

console.log("Storing file in", path);

//Storing file in /storage/emulated/0/downloaded_content/download.zip

this.downloadTask = RNFetchBlob .config({ IOSBackgroundTask: true, addAndroidDownloads: { useDownloadManager: true, notification: true, mime: 'application/zip', path: path }, path: path, appendExt: 'zip' }) .fetch('GET', URL) .progress({ count: 10 }, (received, total) => { console.log(Total ${total}, Received ${received});

    this.setState({
      progress: received / total
    })
  });

this.downloadTask.then((res) => {
  console.log('The file saved to ', res.path())
  this.changeState(constants.STATES.DOWNLOAD_COMPLETE, {
    downloadFilePath: res.path()
  });
}).catch((error) => {
  if (error.message === "cancelled") {
    this.changeState(constants.STATES.DOWNLOAD_CANCELLED);
  } else {
    this.changeState(constants.STATES.DOWNLOAD_ERROR);
  }
});

`

Download gets stuck in queued state, after debugging using android studio, download manager throws Path apperars to be invalid, this is not handled in module and thus our app gets stuck in downloading state, no notification is shown as well.

rabanos-rvc commented 5 years ago

up

ghost commented 4 years ago

+1 @rabanos-rvc @teamtechalpha did you people manage to solve it?

vinitraj10 commented 3 years ago

Actually, the problem lies in DownloadManager rather than RN-fetch blob itself, so the basic problem is that Rn fetch blob use download manager to do downloading but the issue is with whenever your download API throws an exception with status code 500, instead of returning BroadCastReceiver they again retry that request until a maximum retry count, so if you will leave the enqueued download for some time, you would see that Rn fetch blob will resolve that promise.

So In short a workaround from the given Stackoverflow question could be in our RnFetchBlobReq.java we can make some modification where we can query download manager at some interval and we can handle if DownloadManager.STATUS_PAUSED then we can resolve the promise. Because I found out that whenever Api returns 500 download manager waits to retry which has this value.so it can solve our problem if our API is returning 500 server error,but i don't know if its a reliable solution or not.but it worked for me,I ll be posting code snippet in a while.

Also one quick solution could be without changing your code,is you can check at the API end and make sure your returning status code 200 400 etc but not 500!!

For reference - https://stackoverflow.com/questions/39745724/downloadmanager-broadcastreceiver-not-called-on-a-500-error-waiting-to-retry https://stackoverflow.com/questions/34782965/downloadmanager-understanding-error-http-data-error