Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  182] [ 7]  / answers: 1 / hits: 24268  / 10 Years ago, tue, august 5, 2014, 12:00:00

Given I have a file without extension appended to its name, ex: images/cat_photo



Is there a method in Node.js to extract MIME type of a given file? Module mime in this case does not work.


More From » node.js

 Answers
20

Yes, there is a module called mmmagic. It tries best to guess the MIME of a file by analysing its content.



The code will look like this (taken from example):



var mmm = require('mmmagic'),
var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);

magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
});


But keep in mind, that the guessing of a MIME type may not always lead to right answer.



Feel free to read up on types signatures on a wiki page.


[#69909] Saturday, August 2, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
erick

Total Points: 588
Total Questions: 92
Total Answers: 100

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
;