Getting mime types of files uploaded to your website is easy. You can use the HttpPostedFile.ContentType Property, but, what happens when HttpPostedFile is not available to you? You can use your computer’s registry in order to look at a file’s extension and fine the associated mime type.
public static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
Posted
27 Nov 2009 8:39 AM
by
Gal Ratner