React-native-fs: How can I tell if file is completed download and ready to be viewed?

Created on 14 Sep 2018  路  5Comments  路  Source: itinance/react-native-fs

How can I tell if file is completely finished downloading and ready to be viewed?

I am downloading images from server and placing them in the DocumentDirectoryPath, however, even after the download has finished (looped with downloadFile()) I noticed that the image may appear corrupt unless I use a long timeout before displaying.

Most helpful comment

I have some issues like yours @beisert1
My code follows

openFile = async file => {
    try{
      const filePath = `${RNFS.DocumentDirectoryPath}/${file.title}`;

      await RNFS.downloadFile({
        fromUrl: file.url,
        toFile: filePath
      });

      await FileViewer.open(filePath);

    }catch(e){
      console.log("Unsupported file");
    }

All 5 comments

Sometimes after viewing a downloaded image from the DocumentDirectory it will look corrupted (half gray image), once I hard close and open back up the image looks fine. So my thought was is that the image is not done writing to the directory when it is being viewed.

Hi can you provide some code that you're using to "looped with downloadFile()".

The way you'll handle the promise may be related to your issue. But we can't tell anything with juste the behaviour.

A way to go is maybe download a ZIP file instead images directly, then unzip it at the right location.

I have some issues like yours @beisert1
My code follows

openFile = async file => {
    try{
      const filePath = `${RNFS.DocumentDirectoryPath}/${file.title}`;

      await RNFS.downloadFile({
        fromUrl: file.url,
        toFile: filePath
      });

      await FileViewer.open(filePath);

    }catch(e){
      console.log("Unsupported file");
    }

We have same issue. Does anybody know any solution?

this way will wait the file to finish downloading.

downloadFile = async (file) => {
await RNFS.downloadFile({
fromUrl: file.url,
toFile: filePath
}).promise;
};

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AgNm picture AgNm  路  4Comments

JohnRyanTsai picture JohnRyanTsai  路  3Comments

AbdelkaderOumrani picture AbdelkaderOumrani  路  3Comments

let-aurn picture let-aurn  路  3Comments

npomfret picture npomfret  路  4Comments