React native: how to get file size, mime type and extension?
39,571
Solution 1
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) => {})
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
}
Solution 2
In react-native-fs
you can use the stat
method (to get the file size)
import { stat } from 'react-native-fs';
...
const statResult = await stat('path/to/my/file.txt');
console.log('file size: ' + statResult.size);
Solution 3
File size: This answer suits best with new CRNA client. Use File System from Expo.
import {FileSystem} from expo
getFileSize = async fileUri => {
let fileInfo = await FileSystem.getInfoAsync(fileUri);
return fileInfo.size;
};
Solution 4
You can get data about the blob with react-native-fetch-blob and use react-native-mime-types to get the extension
const res = yield RNFetchBlob.config({fileCache: false}).fetch('GET', url, {})
const blob = yield res.blob().then((blob) => {
mimetype.extension(blob.type)
}
)
Author by
Mr. B.
Updated on July 09, 2022Comments
-
Mr. B. almost 2 years
I know react-native-fs and react-native-fetch-blob, but I'm missing simple helper functions like
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);
What's the proper way to get the file size, mime type and extension?
Thanks in advance!