Tuesday, November 5, 2013

Determine the mime type of a file based on the file header signatures not the extension

Most image file formats have unique bytes at the start. The unix file command looks at the start of the file to see what type of data it contains.

http://en.wikipedia.org/wiki/List_of_file_signatures
http://magicdb.org/

A comprehensive site of file formats is available at:
http://www.wotsit.org

If you're using .NET Framework 4.5 or above, there is a now a MimeMapping.GetMimeMapping(filename) method that will return a string with the correct Mime mapping for the passed filename.

Documentation is at http://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping

Windows DLL Urlmon.dll is capable of determining the MIME type of a given data stored in memory, considering the first 256 bytes of the byte array, where such data is stored.

http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature?rq=1
http://stackoverflow.com/questions/15300567/alternative-to-findmimefromdata-method-in-urlmon-dll-one-which-has-more-mime-typ/15595571#15595571
http://stackoverflow.com/questions/52739/is-there-a-way-to-infer-what-image-format-a-file-is-without-reading-the-entire

No comments: