joltup / rn-fetch-blob

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

Possible Unhandled Promise Rejection (id: 0) #677

Open NSR88 opened 3 years ago

NSR88 commented 3 years ago

Here is the code, I used ----

var filename = fileURL.substring(fileURL.lastIndexOf("/") + 1);
    // send http request in a new thread (using native code)
    RNFetchBlob.fetch("GET", fileURL, {})
      .then(res => {
        let status = res.info().status;

        if (status == 200) {
          // the conversion is done in native code

          console.log("RNFB, file exists");

          const { config, fs } = RNFetchBlob;
          let DownloadDir =
            Platform.OS === "android"
              ? fs.dirs.DownloadDir
              : fs.dirs.DocumentDir;
          var options2 = {
            overwrite: true,
            timeout: 30000,
            addAndroidDownloads: {
              useDownloadManager: true, // setting it to true will use the device's native download manager and will be shown in the notification bar.
              notification: true,
              path:
                DownloadDir +
                "/App_name/" +
                this.state.selectedMonthToServer +
                "_" +
                filename, // this is the path where your downloaded file will live in
              description: "Downloading Payslip."
            },
            path:
              DownloadDir +
              "/App_name/" +
              this.state.selectedMonthToServer +
              "_" +
              filename
          };
          RNFetchBlob.config(options2)
            .fetch("GET", fileURL, {})
            .then(res => {
              // the path should be dirs.DocumentDir + 'path-to-file.anything'
              console.log("The file saved to here " + res.path());

              this.ShowHideActivityIndicator();
              console.log("The file saved to " + res.path());
              Alert.alert(
                "App-Name",
                "Payslip downloaded. Do you want to open?",
                [
                  {
                    text: "Open",
                    onPress: () =>
                      Platform.OS === "android"
                        ? RNFetchBlob.android.actionViewIntent(
                            res.path(),
                            "application/pdf"
                          )
                        : RNFetchBlob.ios.openDocument(res.path())
                  },
                  {
                    text: "Cancel",
                    onPress: () => console.log("cancel pressed")
                  }
                ],
                { cancelable: true }
              );
            })
            .catch((errorMessage, statusCode) => {
              // This is never hit for a server 404 error
              console.log("error1","error1")
              console.log(errorMessage, statusCode);
              this.ShowHideActivityIndicator();
              Alert.alert(
                "App-Name",
                "Payslip not available.",
                [{ text: "OK" }],
                { cancelable: true }
              );
            });
        } else {
          // handle other status codes
          console.log("RNFB, file doesn't exist");
          this.ShowHideActivityIndicator();
          Alert.alert(
            "App-Name",
            "Payslip not available.",
            [{ text: "OK" }],
            { cancelable: true }
          );
        }
      })
      // Something went wrong:
      .catch((errorMessage, statusCode) => {
        // error handling
        console.log("error2","error2")
        console.log(errorMessage, statusCode);

        this.ShowHideActivityIndicator();
        Alert.alert(
          "App-Name",
          "Payslip not available.",
          [{ text: "OK" }],
          {
            cancelable: true
          }
        );
      });

This is how, my code looks like.

The response, for this, I am getting as --- 2020-09-25 16:58:47.317 23737-24227/com.appname I/ReactNativeJS: RNFB, file exists 2020-09-25 16:58:53.343 23737-24227/com.appname W/ReactNativeJS: Possible Unhandled Promise Rejection (id: 0): "Request timed out"

NSR88 commented 3 years ago

For Android 9 (Pie), its working fine with https url, but not working with http url. For other versions, even http url is working.

usamaehsan commented 3 years ago

@NSR88 have you found any solution?

Linuhusainnk commented 3 years ago

any solution