Site icon Ryadel

Get a File Content-Type / MIME-type from file extension in ASP.NET C#

Classe ASP.NET C# per il controllo e il calcolo formale del Codice Fiscale

How can I get the MIME type from a file extension in C#? This is a rather common question among developers, an evergreen requirement that I happen to heard at least once a year from friends & colleagues working with ASP.NET MVC,ASP.NET Web API and (lately) .NET Core. The reason is pretty much obvious: whenever you end up working with file object storage in any web-based or client-based application, you will sooner or later have to retrieve the MIME type related to the byte array you're dealing with.

There are a number of ways/techniques to do that, but - for the sake of simplicity - we will put them down to two: looking them up within the Windows Registry or relying to static, hard-coded MIME type lists. We won't consider anything that involves querying an external service, as we do want an efficient way to deal with such issue.

Using the Windows Registry

The main benefit of this technique is the simplicity: you will get the job done in few lines of code.

However, there is a major caveat: your look-up process will be limited to those MIME types registered within the Windows OS installed on the machine hosting your application. Meaning that, as long as you web server doesn't support - for example - PDFs, you won't get the MIME type for PDF files. This is undoubtely a major issue that might leads some to prefer the following method instead.

Using a static MIME Type Map

To overcome the big limitation of the previous method you can use a manually-built static MIME map containing the MIME types for the most common / used / popular file extensions. There are many of them available throughout the web: I used to have mine too, until I found this great GitHub project that covers a gigantic amount of them: it also includes an efficient and deterministic two-way mapping, so you can also use it the other way around (retrieve the extension from a given MIME type).

Here's the source code updated up to 31 Jan, 2017:

The only issue I have with this technique is that it requires a TON of static code: that's most expected, since everything you're going to need is hard-coded inside the class. This also means that you will have to manually update the class anytime a new MIME type if released - unless you don't want to support it.

Luckily enough the author now also keeps an updated NuGet package that - if you're willing to add the dependance to your project - will keeps everything up-to-date without having to worry about it anymore.

A big thanks to Samuel Neff for the great work!

UPDATE: a comment by Martino Bordin, which we would like to thank, points out that ASP.NET 4.5 Framework introduced a new public method to the System.Web namespace - System.Web.MimeMapping.GetMimeMapping - which finally exposes the System.Web.MimeMapping sealed dictionary, which contains a whopping list of 342 known MIME Types! If you're using (or willing to use) the 4.5 Framework you can definitely use that native method without adding external dependencies self-made classes, yet if you aren't... You can (almost) do the same thing with a small "hack" you can implement using the System.Reflections namespace: the credits for such technique go to this awesome post from David Haney, which happens to be the Engineering Manager of the StackOverflow web portal.

Here's the source code of his MimeMappingStealer class, which basically exposes sealed dictionary within a public method using an Invoke:

A big thank to Martino and David for saving the day!

 

Exit mobile version