Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native: how to get file size, mime type and extension?

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!

like image 353
Mr. B. Avatar asked Dec 29 '16 01:12

Mr. B.


People also ask

How does react native determine file size?

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

How do I get the size of a video in react native?

There is not direct way to get it done. You will need to check the file size and show alert if it is not what you expect. You can get the file size with rn-fetch-blob package. You can use react-native-fs as well.

What is the extension of react native file?

You can also use the . native. js extension when a module needs to be shared between NodeJS/Web and React Native but it has no Android/iOS differences. This is especially useful for projects that have common code shared among React Native and ReactJS.


1 Answers

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
}

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

like image 149
mfekim Avatar answered Oct 11 '22 16:10

mfekim