I am trying to determine dynamically the content/type of a input file. If I would be in a windows application I could write code like this (from this blog)
private string GetContentType(string fileName) {
string contentType = "application/octetstream";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (registryKey != null && registryKey.GetValue("Content Type") != null)
contentType = registryKey.GetValue("Content Type").ToString();
return contentType;
}
What other methods are more suitable for an MVC application?
I would like to use the param within the Controller.File(...)
method that receive a filepath and a contentype.
In .Net 4.5 you can use MimeMapping.GetMimeMapping
:
string contentType = MimeMapping.GetMimeMapping("someFileName.pdf")
// contentType = "application/pdf"