Convert file extension to MIME type
Sometimes in a web application you need to upload files to the server and then download them to the client. To send a file to the client for downloading we need to specify a MIME type into the response content type parameter. This way we provide an information to the client browser what is the type content type of the output stream. In the application that I work at the moment we need to upload different file types and then to allow the end-user to download them. For this purpose I created a class that converts file extension to a MIME type. I used two sources for information about file extensions and the corresponding MIME type – MIME Type Detection in Internet Explorer and Office 2007 File Format MIME Types for HTTP Content Streaming.
1: public static class FileExtensionConverter
2: {
3: private static IDictionary<string, string> extensionMIMETypeMapping;
4: private static string defaultMIMEType = "application/octet-stream";
5:
6: static FileExtensionConverter()
7: {
8: extensionMIMETypeMapping = new Dictionary<string, string>()
9: {
10: { "txt", "text/plain" },
11: { "rtf", "text/richtext" },
12: { "wav", "audio/wav" },
13: { "gif", "image/gif" },
14: { "jpeg", "image/jpeg" },
15: { "png", "image/png" },
16: { "tiff", "image/tiff" },
17: { "bmp", "image/bmp" },
18: { "avi", "video/avi" },
19: { "mpeg", "video/mpeg" },
20: { "pdf", "application/pdf" },
21: { "doc", "application/msword" },
22: { "dot", "application/msword" },
23: { "docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
24: { "dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template" },
25: { "xls", "application/vnd.ms-excel" },
26: { "xlt", "application/vnd.ms-excel" },
27: { "csv", "application/vnd.ms-excel" },
28: { "xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
29: { "xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template" },
30: { "ppt", "application/vnd.ms-powerpoint" },
31: { "pot", "application/vnd.ms-powerpoint" },
32: { "pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
33: { "potx", "application/vnd.openxmlformats-officedocument.presentationml.template" }
34: };
35: }
36:
37: public static string ToMIMEType(string extension)
38: {
39: if (extension == null || extension.Length == 0)
40: {
41: return defaultMIMEType;
42: }
43:
44: string lowerExtension = extension.ToLower();
45: string mime;
46: if (!extensionMIMETypeMapping.TryGetValue(lowerExtension, out mime))
47: {
48: mime = defaultMIMEType;
49: }
50:
51: return mime;
52: }
53: }
You can see that it is not a rocket science but I thought that it may be useful to have it ready and to write it from scratch. In this class a dictionary is used to store the mapping and it is initialized in a static constructor with hard coded mapping information. For sure, a better solution is to store this information outside of the application in a store such as a database table or xml file or even a simple plain text file. If the type is not in the mapping it returns a default mapping that is “application/octet-stream”.
April 23rd, 2010 at 6:23 pm
[...] Convert file extension to MIME type [...]