在React Native中检查可用磁盘空间

4
我已经实现了以下代码:
  1. 使用RNFS.downloadFile()下载zip文件
  2. 使用ZipArchive.unzip()解压缩文件
  3. 使用RNFS.unlink()删除zip文件
我可以从服务器发送信息,指示zip文件的大小和解压缩目录的大小。但是,如何检测设备上是否有足够的空间来下载和解压缩文件呢?我假设一旦我弄清楚这一点,就可以进行如下检查:
if (free_space_on_device > zip_size + unpacked_size){
   proceed with steps 1 through 3 (listed above)
}
2个回答

6

我没有意识到RNFS有一个名为getFSInfo的函数。有了这个知识,我可以直接使用以下命令:

RNFS.getFSInfo()
.then ((info) => {
   console.log("Free Space is" + info.freeSpace + "Bytes")
   console.log("Free Space is" + info.freeSpace / 1024 + "KB")
})

1
RNFS.getFSInfo 不需要参数: https://github.com/itinance/react-native-fs#getfsinfo-promisefsinforesult 看起来它只返回主存储的信息。 - Marc_Alx
@Marc_Alx 没错,谢谢你提供的信息! - kojow7

2

如果您正在使用 react-native-fetch-blob 而不是 react-native-fs,那么这就是您要寻找的函数:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接