React native:如何获取文件大小,mime类型和扩展名?

Mr.*_* B. 12 javascript android ios react-native

我知道react-native-fsreact-native-fetch-blob,但是我缺少简单的辅助函数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);
Run Code Online (Sandbox Code Playgroud)

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

提前致谢!

小智 15

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

RNFetchBlob.fs.stat(PATH_OF_THE_TARGET)
.then((stats) => {})
.catch((err) => {})
Run Code Online (Sandbox Code Playgroud)

响应统计信息包含以下信息:

{
     // 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
}
Run Code Online (Sandbox Code Playgroud)

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

  • 哑剧类型怎么样? (3认同)

Pet*_*ill 9

react-native-fs您可以使用该stat方法(获取文件大小)

import { stat } from 'react-native-fs';

...

const statResult = await stat('path/to/my/file.txt');
console.log('file size: ' + statResult.size);
Run Code Online (Sandbox Code Playgroud)

  • 文件的 mime-type 怎么样? (2认同)

小智 5

文件大小:此答案最适合新的 CRNA 客户端。使用Expo 的文件系统

import {FileSystem} from expo 

getFileSize = async fileUri => {
   let fileInfo = await FileSystem.getInfoAsync(fileUri);
   return fileInfo.size;
 };
Run Code Online (Sandbox Code Playgroud)