Is there a way to get ALL the MIME types instead of wrinting a huge case statement?

I want to populate

Response.ContentType = "text/plain";

From somewhere in the server/web/dictionary ALL possible MIME types according to file extension:

public string GetMimeType(string extension)
{
    //This is what I am looking for.    
}

Also, I have to rename the file (at least if going to be downloaded, so I have to know in advance if it’s going to be opened or not.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can store the mimetype when the file is uploaded ( FileUpload.PostedFile.ContentType ) and send that when the file is requested.

Method 2

Umm… why? You’re not going to be returning content of every possible type, are you?

Here’s a list of common types: http://www.webmaster-toolkit.com/mime-types.shtml. There is no list that would include “ALL” types simply because any application vendor can create a custom one and associate it with a custom extension.

Method 3

It’s going to depend on your platform. Here’s one for C# and IIS: http://blog.crowe.co.nz/archive/2006/06/02/647.aspx

In Powershell it’s a one-liner:

([adsi]"IIS://localhost/MimeMap").MimeMap

Method 4

The code in the link posted by Richard:

// Maintain a sorted list to contain the MIME Types
SortedList sl = new SortedList();
Console.WriteLine("IIS Mime Map - c#");
Console.WriteLine();
// Serve to connect to...
string ServerName = "LocalHost";
// Define the path to the metabase
string MetabasePath = "IIS://" + ServerName + "/MimeMap";
// Note: This could also be something like
// string MetabasePath = "IIS://" + ServerName + "/w3svc/1/root";
try
{
  // Talk to the IIS Metabase to read the MimeMap Metabase key
  DirectoryEntry MimeMap = new DirectoryEntry(MetabasePath);
  // Get the Mime Types as a collection
  PropertyValueCollection pvc = MimeMap.Properties["MimeMap"];
  // Add each Mime Type so we can display it sorted later
  foreach (object Value in pvc)
  {
    // Convert to an IISOle.MimeMap - Requires a connection to IISOle
    // IISOle can be added to the references section in VS.NET by selecting
    // Add Reference, selecting the COM Tab, and then finding the 
    // Active DS Namespace provider
    IISOle.MimeMap mimetypeObj = (IISOle.MimeMap)Value;
    // Add the mime extension and type to our sorted list.
    sl.Add(mimetypeObj.Extension, mimetypeObj.MimeType);
  }
  // Render the sorted MIME entries
  if (sl.Count == 0)
    Console.WriteLine("No MimeMap entries are defined at {0}!", MetabasePath);
  else
    foreach (string Key in sl.Keys)
      Console.WriteLine("{0} : {1}", Key.PadRight(20), sl[Key]);
}
catch (Exception ex)
{
  if ("HRESULT 0x80005006" == ex.Message)
    Console.WriteLine(" Property MimeMap does not exist at {0}", MetabasePath);
  else
    Console.WriteLine("An exception has occurred: n{0}", ex.Message);
}

Method 5

// Convert to an IISOle.MimeMap – Requires a connection to IISOle
// IISOle can be added to the references section in VS.NET by selecting
// Add Reference, selecting the COM Tab, and then finding the
// Active DS Namespace provider

According to my googling: (lost the links, sorry)

The “Active DS IIS Namespace Provider” is part of the IIS installation.
After you install IIS you will see that in the list of options.
If you don’t see it should be located at C:windowssystem32inetsrvadsiss.dll.

To install IIS:
click Start, Settings, Control Panel, Add or Remove Programs, Add or Remove Windows Components, select Internet Informatoin Services (IIS).

Most of the code I’ve seen uses some combination of these:

using System.IO;
using System.DirectoryServices; // Right-click on References, and add it from .NET
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;
using IISOle;
using System.Collections.Specialized;

The Active DS Namespace might be under the COM tab when adding the reference.

Method 6

I’ve written a small class based on the webmaster-toolkit.com list. This is to avoid using COM and the IIS route or any IIS references.

It uses an XML serialized list which contains about 400 mimetypes, so is usually more than enough unless you have really obscure mimetypes. In that case you can just add to the XML file.

The full solution can be found here. Here’s a sample:

class Program
{
    static void Main(string[] args)
    {
        var list = MimeType.Load();
        MimeType mimetype = list.FirstOrDefault(m => m.Extension == "jpg");
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x