Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104741

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.

Upvotes: 2

Views: 6565

Answers (6)

Chris S
Chris S

Reputation: 65436

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");
    }
}

Upvotes: 1

TomEberhard
TomEberhard

Reputation: 1091

// 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:\windows\system32\inetsrv\adsiss.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.

Upvotes: 1

Colin
Colin

Reputation: 10638

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);
}

Upvotes: 2

Paul van Brenk
Paul van Brenk

Reputation: 7549

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

Upvotes: 4

Richard Berg
Richard Berg

Reputation: 20784

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

Upvotes: 2

ChssPly76
ChssPly76

Reputation: 100706

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.

Upvotes: 4

Related Questions