private 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;
}
KC, this is brilliant! Thank you for this solution. I've been digging around for something like this, but available information has been sparse. I found another solution, but it was very long and convoluted, and written in VB. This solved my problem perfectly!
ReplyDeleteNice job, but it's possible not to use the Windows Register?? I ask this because it's possible to publish the ASP.NET pages on non-Windows OS.
ReplyDeletevery, very, veryvery nice. smart, simple, straightforward. perfect! thx for ending my seemingly endless search ;o)
ReplyDeleteI am glad I could help some of you. And, thanks for your kind comments.
ReplyDeleteTo answer José's question: Basically we need some kind of table or data store that would have the translation between the file name extension and it's mime type. Fortunately, Windows Registry has that info, and the post above shows how that could be done. In non-windows systems, you need to find a similar translation table, and then you could use a dataset and search on it. But unfortunately I am not the right person in non-Windows domain. Sorry, I wish I could have a better answer for you.
For non-Windows use, there is the MimeTypes.xml solution (often used in Java or PHP). Several .NET classes are already available on the www.
ReplyDeleteThe Windows Registry solution is quite simple and avoid extra work to keep the translation table up to date and coherent. Unfortunatly, using the registry also means the file extensions have to be known by Windows, which often imply some client applications to be installed on the server (it probably works if those applications have been previously installed, then removed)
This solution has a problem that i is limited to the softwares you have installed on the machine. For example, if PDF is not installed, then you cannot get the MIME type for ".pdf" using this technique.
ReplyDeleteHi, is there any way to find the mime type without the file name, I mean my application receive a file without extension y will appreciate your help thank you.
ReplyDeleteSO BRILLIANT!
ReplyDeleteThanks, It's perfect solution what I looking for!
--
Anon, just try regist .pdf type @ HKCR/.pdf
---------------------------------
(default) = AcroExch.Document
Content Type = application/pdf
---------------------------------
and do every ext/type you needs.
:)
thanks. works great
ReplyDeletethis is reallly nice
ReplyDeletethanks a lot
bye
Excellent solution! very elegant! For all those who want to include an if and else... copy the table on http://technet.microsoft.com/en-gb/library/bb742440.aspx save as xml or in database and query there as well.
ReplyDeleteYou da man!
ReplyDeleteTy
ReplyDeleteThis is a real life saver, as flash has the unfortunate side effect of setting all of my uploaded files to "application/octet-stream". Am I right in thinking this relies on the server having the correct applications installed to recognise the mime types?
ReplyDeleteThanks ... Help me a Lot ....
ReplyDeleteExactly what I needed. Excellent work... Thanks!
ReplyDeletePretty clean...nice. I wonder if there is a web service or something that may be more reliable?
ReplyDeleteThanks,
ReplyDelete