本文介绍了React Native:如何获取文件大小、mime 类型和扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道 react-native-fs 和 react-native-fetch-blob,但我缺少简单的辅助函数,例如 getFileInfo(file).
I know react-native-fs and react-native-fetch-blob, but I'm missing simple helper functions like
getFileInfo(file)
.
所需的伪代码:
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 类型和扩展名的正确方法是什么?
What's the proper way to get the file size, mime type and extension?
提前致谢!
推荐答案
使用react-native-fetch-blob,你可以通过以下代码获取文件大小:
With react-native-fetch-blob, you can get the file size with the code below:
RNFetchBlob.fs.stat(PATH_OF_THE_TARGET)
.then((stats) => {})
.catch((err) => {})
响应 stats 包含以下信息:
The response stats contains the following info:
{
// 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
}
这篇关于React Native:如何获取文件大小、mime 类型和扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!