Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using Expo for React Native, is there any way to read the mime-type from a file?

React Native - File Type is great but needs to be linked, and thus, won't work with a managed Expo project.

How can you read the file mime type when using Expo managed projects?

like image 529
GollyJer Avatar asked Nov 16 '22 18:11

GollyJer


1 Answers

You can simply use the mime Javascript library to get the mime-type from the file name: https://www.npmjs.com/package/mime

import * as mime from 'mime';

const mimeType = mime.getType('my-doc.pdf') // => 'application/pdf'

If you are using the DocumentPicker to get the file, you can obtain the file name from the result directly:

const result = await DocumentPicker.getDocumentAsync();
if (result.type === 'cancel') { 
  return;
}
const fileName = result.name;
const mimeType = mime.getType(fileName);
like image 61
Jose Manuel Báez Soriano Avatar answered Jun 09 '23 18:06

Jose Manuel Báez Soriano