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 773 forks source link

Downloaded file with status 200 but file doesn't exist in Android 10 emulator #485

Open olcayertas opened 4 years ago

olcayertas commented 4 years ago

I am downloading a XLSX file on an Android 10 emulator (Pixel 3a API 29). Operation ends with HTTP status code 200. Here is the response headers:

{
  "Server": "Gozen Web Server",
  "Accept-Ranges": "bytes",
  "Content-Disposition": "attachment; filename=2019_11_04_05_11_2019_sali_Iga_saatler_5dc0466c121e1.xlsx; filename*=utf-8''2019_11_04_05_11_2019_sali_%C4%B0ga_saatler_5dc0466c121e1.xlsx",
  "Last-Modified": "Mon, 04 Nov 2019 15:40:27 GMT",
  "Date": "Tue, 05 Nov 2019 07:26:32 GMT",
  "Cache-Control": "public",
  "Connection": "close",
  "Content-Length": "88156",
  "Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}

Here ismy download code:

function download(url, completion: (response: Response) => {}) {

  getTokenAsync().then(value => {
    if (value === null) {
      showMessage("Failed to get token!", null);
      return;
    }
    let headers = { Authorization: "Bearer " + value };
    let options = { fileCache: true };
    console.info("Downloading: " + url);
    RNFetchBlob.config(options).fetch("GET", url, headers).then(response => {
      let status = response.info().status;
      console.log(status + " " + url);
      if (status === 200) {
        let dir = Platform.OS === "ios" ?
          RNFetchBlob.fs.dirs.DocumentDir :
          RNFetchBlob.fs.dirs.DownloadDir;
        let sourcePath = response.path();
        let destinationPath = dir + "/" + getFileNameFromResponseAndUrl(response, url);
        RNFetchBlob.fs.exists(sourcePath).then(value => {
          console.log("download: Source path " + value);
          RNFetchBlob.fs.exists(destinationPath).then(value => {
            if (value) {
              RNFetchBlob.fs.unlink(destinationPath).then(() => {
                moveAndOpen(response.path(), destinationPath);
              });
            } else {
              moveAndOpen(response.path(), destinationPath);
            }
          });
        }).catch(reason => {
          console.log(reason);
        });
      } else if (status === 404) {
        showMessage("Dosya bulunamadı!", null);
      } else if (status > 499 && status < 600) {
        showMessage("Sunucu hatası!", null);
      } else {
        showMessage("Dosya indirilemedi! " + status);
      }
      completion(response);
    }).catch(reason => {
      //console.error(JSON.stringify(reason, null, 2));
      showMessage(reason);
      completion(null);
    });
  });
}

File seem to be download to this path: /data/user/0/com.gozensahamobile/files/RNFetchBlobTmp_1h7oxinvsiltpzzeb7khc and RNFetchBlob.fs.exists function returns true but when I look at the application's files folder with file explorer of emulator there is no file and my move code fails:

export function moveAndOpen(source, destination) {
  RNFetchBlob.fs.mv(source, destination).then(() => {
    open(destination);
  }).catch(reason => {
    console.error("moveAndOpen: " + reason);
  });
}

This code was working good before. Can anyone have the same problem?

fengzhongye commented 4 years ago

Have you solved it? I have the same problem

gbferreira commented 4 years ago

I have a similar problem, when android loose connection return status 200 but file was broked. I'm using RN 0.10.5, have you seen an issue about it ?

vajid-khan commented 3 years ago

@olcayertas have you solved the issue? I am facing similar issue with version 0.12.0, RN version 0.62.0. I am getting file exists true but file is not visible in file explorer