Pages

Thursday, February 27, 2014

Get MIME Type by file extension

I was searching for a way to get the MIME type of a file by file extension and .NET Framework 4.5 comes to the rescue.

You can make use of API GetMimeMapping which is found in the .NET Framework 4.5 System.Web.dll

Here is the documentation of it - http://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping.aspx

Example on how you can use it

string mimeType = System.Web.MimeMapping.GetMimeMapping(fileName);

If you are running your ASP.NET web application in the integrated mode in IIS then this API actually uses the list of MIME types in the IIS and so seems to me that even if your IIS MIME types list gets upgraded, you dont need to update your code, huh..cool .. isn't it?

Hope you find this useful.

Reference - http://blogs.msdn.com/b/webdev/archive/2013/02/14/getting-a-mime-type-from-a-file-extension-in-asp-net-4-5.aspx

No comments:

Post a Comment