【问题标题】:Checking for available disk space in React Native在 React Native 中检查可用磁盘空间
【发布时间】:2017-01-23 04:24:03
【问题描述】:
我已经实现了以下代码:
- 使用 RNFS.downloadFile() 下载 zip 文件
- 使用 ZipArchive.unzip() 解压缩文件
- 使用 RNFS.unlink() 删除 zip 文件
我可以从服务器发送信息,指示 zip 文件有多大以及解压后的目录有多大。但是,如何检测设备上是否有足够的空间来下载和解压缩文件?我假设一旦弄清楚这一点,我就可以像这样进行检查:
if (free_space_on_device > zip_size + unpacked_size){
proceed with steps 1 through 3 (listed above)
}
【问题讨论】:
标签:
android
ios
react-native
diskspace
【解决方案1】:
我没有意识到 RNFS 有一个名为 getFSInfo 的函数。有了这些知识,我可以发出以下命令:
RNFS.getFSInfo()
.then ((info) => {
console.log("Free Space is" + info.freeSpace + "Bytes")
console.log("Free Space is" + info.freeSpace / 1024 + "KB")
})
【解决方案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);
} );
在 Android 上,响应对象如下所示:
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);
} );