我知道react-native-fsreact-native-fetch-blob,但我缺少简单的助手函数,比如getFileInfo(file).

Desired pseudo code:

let fileInfo = getFileInfo('path/to/my/file.txt');
console.log('file size: ' + fileInfo.size);
console.log('mime type: ' + fileInfo.type);
console.log('extension: ' + fileInfo.extension);

获取文件大小、mime类型和扩展名的正确方法是什么?

提前谢谢!

推荐答案

使用react-native-fetch-blob,您可以通过以下代码获得文件大小:

RNFetchBlob.fs.stat(PATH_OF_THE_TARGET)
.then((stats) => {})
.catch((err) => {})

响应stats包含以下信息:

{
     // file name
     filename : 'foo.png',
     // folder of the file or the folder itself
     path : '/path/to/the/file/without/file/name/',
     // size, in bytes
     size : 4901,
     // `file` or `directory`
     type : 'file',
     // last modified timestamp
     lastModified : 141323298
}

资料来源:https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#user-content-statpathstringpromisernfetchblobstat

React-native相关问答推荐

添加REACT-Native-SVG时出现错误-失败:构建失败并出现异常

我们如何才能将我们自己的应用程序(IOS)排除在共享表中?

找不到变量:firebase/react native(expo)上的 IDBIndex

ReactJS 应用多种内联样式

React Native WebView onMessage 没有做任何事情

无法在 React Native iOS 模拟器中按 URI 显示图像

如何获取 React Native 应用程序的崩溃日志(log)?

React Native Card Carousel 视图?

如何在react-native android中打开应用程序设置页面?

命令失败:gradlew.bat installDebug error whenever installing dependencies like navigation, firebase, icons etc in React-Native

React-Native iOS - 如何通过按下按钮从 React-Native 视图导航到非 React-Native 视图(本机 iOS 视图控制器)?

如何在 react-native 中从 AsyncStorage 中删除元素

为什么,确切地说,我们需要 React.forwardRef?

HighLight / ScrollTo - React-Native 上的 ListView、ScrollView

在 React 中调用 getDerivedStateFromProps 中的方法

带有 React Native 的 EventEmitter 和 Subscriber ES6 语法

如何设置样式属性以填充剩余空间?

如何将图形 API 与 react-native-fbsdk 一起使用?

无法解析模块react-native-screen

如何更改后退按钮标签,react-navigation