Checking for available disk space in React Native
Asked Answered
U

2

5

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)
}
Ulpian answered 23/1, 2017 at 4:24 Comment(0)
U
7

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")
})
Ulpian answered 23/1, 2017 at 5:20 Comment(2)
RNFS.getFSInfo Doesn't take an argument : github.com/itinance/… It seems that it returns info only for the main storage.Inclinometer
@Inclinometer Right you are. Thank you for that information!Ulpian
D
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);
} );
Danaedanaher answered 9/4, 2018 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.