views:

662

answers:

5

Hi

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.

+2  A: 

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.

ChssPly76
Yes I am.I am going to return all the possible types.I need to allow users to upload and download files of ANY type
Shimmy
As I said, there's no such thing as "all" mime types. Your best bet is to get a list of common ones (and you can treat "common" here as wide or as narrow as you want) and return everything else as binary ("application/octet-stream").
ChssPly76
I didn't mean ALL ALL I meant the listed usual ones
Shimmy
Sorry for saying ALL before, I am not so strict with that ALL, the list in the link you provided does well for me.
Shimmy
+1  A: 

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
Richard Berg
This is actually something I am looking for (the asp one), thought the link doesn't work.
Shimmy
THe link works for me, I'll add the code that's in it, if it is what you want, pls mark Richard's answer as the correct one
Colin
+1  A: 

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

pb
Good point.Please take a look again, I added a few lines to my question.
Shimmy
+1  A: 

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);
}
Colin
A: 

// 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.

tom