0

How will I get the Mime Type or Content Type of a Specific file through its file extension? This program is developed in MVC3 Razor Syntax, the code looks like this:

if (System.IO.File.Exists(file))
            return File(file, "application/pdf", id);
        else
            return RedirectToAction("Index");

I want to change the application/pdf to a dynamic value such that it would supply the MIME type through the id or the filename that is sent to the controller.

tereško
  • 56,151
  • 24
  • 92
  • 147
Arjel
  • 439
  • 2
  • 10
  • 15
  • [c# - Using .NET, how can you find the mime type of a file based on the file signature not the extension - Stack Overflow][1] How about FindMimeFromData API? [1]: http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature – takepara Jun 19 '12 at 02:51
  • File extensions do not map to MIME types 1:1. – tripleee Sep 20 '12 at 11:11
  • Here is a similar question: http://stackoverflow.com/questions/1612767/file-extensions-and-mime-types-in-net – maximpa Nov 22 '12 at 22:40
  • In .Net 4.5 you could use `System.Web.MimeMapping.GetMimeMapping()` – maximpa Nov 22 '12 at 23:20

1 Answers1

1

You could look up the registry value at HKCR\<file extension>\Content Type. This value doesn't exist for all file types, but probably does for all the file types you're interested in.

JSK NS
  • 3,076
  • 2
  • 21
  • 40
Andrew Cooper
  • 30,983
  • 4
  • 74
  • 109