react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

How can I tell if file is completed download and ready to be viewed?

Open beisert1 opened this issue 7 years ago • 7 comments

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.

beisert1 avatar Sep 14 '18 19:09 beisert1

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.

beisert1 avatar Sep 14 '18 20:09 beisert1

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.

jonathangreco avatar Oct 31 '18 14:10 jonathangreco

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");
    }

ricardogobbo avatar Apr 12 '19 04:04 ricardogobbo

We have same issue. Does anybody know any solution?

kaaoo avatar Oct 11 '19 07:10 kaaoo

this way will wait the file to finish downloading.

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

stevenasi avatar Jul 30 '20 06:07 stevenasi

this way will wait the file to finish downloading.

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

what is the purpose of the promise?

imamabdulazis avatar Dec 15 '21 03:12 imamabdulazis

RNFS.downloadFile({ fromUrl: url, toFile, progressInterval: 100, begin: () => { setDownloading(true) console.log('download start') } }).promise.then(() => { console.log('download finished') }) this way works for me, RNFS.downloadFile will return a object contains a promise that gives finished status. same to @stevenasi , thx~

DueTy avatar Jan 18 '22 06:01 DueTy