13

Is there a better way of setting mimetypes in C# than the one I am trying to do thanks in advance.

static String MimeType(string filePath)
{
  String ret = null;
  FileInfo file = new FileInfo(filePath);

  if (file.Extension.ToUpper() == ".PDF")
  {
    ret = "application/pdf";
  }
  else if (file.Extension.ToUpper() == ".JPG" || file.Extension.ToUpper() == ".JPEG")
  {
    ret = "image/jpeg";
  }
  else if (file.Extension.ToUpper() == ".PNG")
  {
    ret = "image/png";
  }
  else if (file.Extension.ToUpper() == ".GIF")
  {
    ret = "image/gif";
  }
  else if (file.Extension.ToUpper() == ".TIFF" || file.Extension.ToUpper() == ".TIF")
  {
    ret = "image/tiff";
  }
  else
  {
    ret = "image/" + file.Extension.Replace(".", "");
  }

  return ret;
}
Julien Poulin
  • 12,737
  • 10
  • 51
  • 76
acadia
  • 2,301
  • 10
  • 40
  • 57
  • 2
    I still don't understand why the large collection of MIME types which MS uses in System.Web hasn't been made available my making the internal class public... everyone is coding their own method to get the mime type of a file name and only very few are actually doing it right. – Lucero Jun 24 '10 at 16:05
  • 1
    possible duplicate of [How do I get the MIME type of a file being requested in ASP.NET C#?](http://stackoverflow.com/questions/1302264/how-do-i-get-the-mime-type-of-a-file-being-requested-in-asp-net-c) – Lucero Jun 24 '10 at 16:08
  • 1
    There's also [Using .NET, how can you find the mime type of a file based on the file signature not the extension. ](http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature) – Roman Jun 24 '10 at 16:12

4 Answers4

15

I got this from this blogpost:

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;
}
VoodooChild
  • 9,776
  • 8
  • 66
  • 99
  • 2
    I was going to suggest the registry. However if you're running in partial trust mode I think you need to explicitly grant registry access else it'll be refused. I'd probably also cache the requests in your app rather than query every time but that's more from general unease of calling out to the registry rather than hard data. Maybe also worth point out: rather than app/unknown, IIS will refuse to serve a file if there's no registry Content Type. – Rup Jun 24 '10 at 16:12
11

Just this one line is needed to do this.

System.Web.MimeMapping.GetMimeMapping(FileName)

Note: It's .NET 4.5+ only

Manoochehr Dadashi
  • 715
  • 1
  • 10
  • 28
  • You might want to include Rup's comment in your answer (link to the docs and that it's 4.5+). – Roman Jan 16 '15 at 15:46
7

A Dictionary<String,String> may prove to be clearer.

private static Dictionary<String, String> mtypes = new Dictionary<string, string> {
        {".PDF", "application/pdf" },
        {".JPG", "image/jpeg"},
        {".PNG", "image/png"},
        {".GIF", "image/gif"},
        {".TIFF","image/tiff"},
        {".TIF", "image/tiff"}
    };

static String MimeType(String filePath)
{
    System.IO.FileInfo file = new System.IO.FileInfo(filePath);
    String filetype = file.Extension.ToUpper();
    if(mtypes.Keys.Contains<String>(filetype))
        return mtypes[filetype];
    return "image/" + filetype.Replace(".", "").ToLower();
}
gimel
  • 83,368
  • 10
  • 76
  • 104
4

If you don't have registry access or don't want to use the registry, you could always use a Dictionary for that.

Dictionary<string,string> mimeTypes = new Dictionary<string,string>() { 
 { ".PDF","application/pdf"},
 { ".JPG", "image/jpeg" },
 { ".JPEG", "image/jpeg" } }; // and so on

Then use:

string mimeType = mimeTypes[fileExtension];

In addition, you could store these mappings in an XML file and cache them with a file dependency, rather than keeping them in your code.

Wim
  • 11,998
  • 1
  • 34
  • 57
  • 1
    Still needs to be hardcoded though, I think the question is about using an existing API rather than having yet another implementation. – Roman Jun 24 '10 at 16:08