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.


A comprehensive site of file formats is available at:

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.


No comments: