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!
let fileInfo = getFileInfo('path/to/my/file. txt'); console. log('file size: ' + fileInfo. size); console.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With