react-native-fs
react-native-fs copied to clipboard
How can I tell if file is completed download and ready to be viewed?
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.
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; };
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?
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~