Checking for available disk space in React Native

对着背影说爱祢 提交于 2019-12-12 03:28:59

问题


I have implemented the following code:

  1. Download a zip file using RNFS.downloadFile()
  2. Unzip the file using ZipArchive.unzip()
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!