Mr.*_* B. 12 javascript android ios react-native
我知道react-native-fs和react-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
在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)
小智 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)
归档时间: |
|
查看次数: |
14202 次 |
最近记录: |