简体   繁体   中英

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!

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

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);

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;
 };

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)
  }
)

To getting file size using react-native-fetch-blob i used the following code.

Its only suitable for small size files. If you have large size files try with some other below answers

For that u need to install base-64

var base64 = require('base-64');
RNFetchBlob.fs.readFile(filePath, 'base64')
    .then((data) => {
        var decodedData = base64.decode(data);
        var bytes=decodedData.length;
        if(bytes < 1024) console.log(bytes + " Bytes");
        else if(bytes < 1048576) console.log("KB:"+(bytes / 1024).toFixed(3) + " KB");
        else if(bytes < 1073741824) console.log("MB:"+(bytes / 1048576).toFixed(2) + " MB");
        else console.log((bytes / 1073741824).toFixed(3) + " GB");
    })

Explanation:

  1. The above code decode the base64 data to string like atob().
  2. Next finding length of string
  3. From that value i have to calculate the file size.

If the file is too large use RNFetchBlob.fs.readStream method instead of RNFetchBlob.fs.readFile

I am getting bytes calculation from SO convert size

The code may be too long to calculate file size. If anyone found easiest way guide me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM