问题
I have implemented the following code:
- Download a zip file using RNFS.downloadFile()
- Unzip the file using ZipArchive.unzip()
- Delete the zip file using RNFS.unlink()
I can send info from the server indicating how big the zip file is and how big the unpacked directory is. However, how can I detect if there is enough space on the device to download and unzip the file? I assume once I figure this out I can just do a check like this:
if (free_space_on_device > zip_size + unpacked_size){
proceed with steps 1 through 3 (listed above)
}
回答1:
I did not realize that RNFS had a function called getFSInfo. With that knowledge, I can just issue the following command:
RNFS.getFSInfo()
.then ((info) => {
console.log("Free Space is" + info.freeSpace + "Bytes")
console.log("Free Space is" + info.freeSpace / 1024 + "KB")
})
回答2:
In case you are using react-native-fetch-blob and not react-native-fs
this is the function you are looking for:
RNFetchBlob.fs.df().then( response => {
console.log('Free space in bytes: ' + response.free);
console.log('Total space in bytes: ' + response.total);
} );
On Android, the response object looks like this:
RNFetchBlob.fs.df().then( response => {
console.log('External free space in bytes: ' + response.external_free);
console.log('External total space in bytes: ' + response.external_total);
console.log('Internal free space in bytes: ' + response.internal_free);
console.log('Internal total space in bytes: ' + response.internal_total);
} );
来源:https://stackoverflow.com/questions/41799211/checking-for-available-disk-space-in-react-native