Tutorial :How can I know if a string represents a valid MIME type?



Question:

I have a whole bunch of string that are supposed to represent MIME types. However, some of these string have bad/invalid MIME types. Is there a way in the .NET framework to get a list of valid MIME types?


Solution:1

Check out this stack overflow post about adding custom mime types.

You should be able to do something like

using (DirectoryEntry mimeMap = new DirectoryEntry("IIS://Localhost/MimeMap"))  {      PropertyValueCollection propValues = mimeMap.Properties["MimeMap"];      foreach(IISOle.MimeMap mimeType in propValues)       //must cast to the interface and not the class      {        //access mimeType.MimeType to get the mime type string.      }  }  


Solution:2

IANA have a list here. I would think that is more of an authority than most lists you can find.


Solution:3

while it's not canonical in the sense of being driven by a standard, the mime.types file delivered with any version of Apache will give you a good idea as to what it (and, therefore, a great deal of the web) thinks are valid MIME types.


Solution:4

Following up from DDaviesBracket, you can find the latest mime.types here:

http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

and then consume the list (e.g. for C#):

  string[] linesOfMimeTypes = File.ReadAllLines("mime.types");    List<string> mimeTypes = new List<string>();  foreach( string line in linesOfMimeTypes )  {      if( line.length < 1 )          continue;      if( line[0] == '#' )          continue;      // else:      mimeTypes.Add( line.Split( new char[] { ' ', '\t' } )[0] );  }    if( mimeTypes.Contains( oneToTest ) )  {      // hooray!  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »