I have a webpage that has FileUpload control to upload files to server. The FileUpload.ContentType is also saved in database and later used in other procedures.
Now I need an equivalent functionality in my webservice. I get the files from user in byte array. But how should I determine the content type? I was stuck on this because there is no native .NET way to get an equivalent of the FileContent of the asp.net FileUpload control.
I tried using the urlmon.dll. But the results for FileUpload.ContentType and the urlmon.dll were different. The FindMimeFromData of urlmon.dll and FileUpload.ContentType return different values for same file.
e.g. For a .csv file, FindMimeFromData returns "text/plain" while the FileUpload ContentType returns "application/vnd.ms-excel".
A detailed discussion about this can be found here: http://www.vbforums.com/showthread.php?t=634382
Finally I decided to build my own class that gets the job done.
The Problem
To build a class that returns the equivalent of asp.net FileUpload control’s ContentType property.
Solution
You can build your own class from the algorithm given here:
http://msdn.microsoft.com/en-us/library/ms775147.aspx
Here’s the class that gives you the equivalent of FileUpload.ContentType property. The MIME types list was picked up from Mendhak’s post in this thread: http://forums.asp.net/t/1143353.aspx. Since the thread is very old, you might need to update the list from other sources that may have a more updated list.
Option Strict On Imports System.Runtime.InteropServices Imports System.IO Public Class MimeTypes Private Shared knownTypes As List(Of String) Private Shared mimeTypes As Dictionary(Of String, String) <DllImport("urlmon.dll", CharSet:=CharSet.Auto)> _ Private Shared Function FindMimeFromData(ByVal pBC As UInt32, <MarshalAs(UnmanagedType.LPStr)> ByVal pwzUrl As String, <MarshalAs(UnmanagedType.LPArray)> ByVal pBuffer As Byte(), ByVal cbSize As UInt32, <MarshalAs(UnmanagedType.LPStr)> ByVal pwzMimeProposed As String, ByVal dwMimeFlags As UInt32, ByRef ppwzMimeOut As UInt32, ByVal dwReserverd As UInt32) As UInt32 End Function Public Shared Function GetContentType(ByVal fileName As String) As String If knownTypes Is Nothing OrElse mimeTypes Is Nothing Then InitializeMimeTypeLists() Dim contentType As String = "" Dim extension As String = IO.Path.GetExtension(fileName).Replace(".", "").ToLower mimeTypes.TryGetValue(extension, contentType) If String.IsNullOrEmpty(contentType) OrElse knownTypes.Contains(contentType) Then Dim headerType As String = ScanFileForMimeType(fileName) If headerType <> "application/octet-stream" OrElse String.IsNullOrEmpty(contentType) Then contentType = headerType End If Return contentType End Function Private Shared Function ScanFileForMimeType(ByVal fileName As String) As String Dim buffer As Byte() = New Byte(255) {} Using fs As New FileStream(fileName, FileMode.Open) Dim readLength As Integer = CInt(Math.Min(256, fs.Length)) fs.Read(buffer, 0, readLength) End Using Try Dim mimeType As UInt32 FindMimeFromData(0, Nothing, buffer, 256, Nothing, 0, mimeType, 0) Dim mimeTypePtr As IntPtr = New IntPtr(mimeType) Dim mime As String = Marshal.PtrToStringUni(mimeTypePtr) Marshal.FreeCoTaskMem(mimeTypePtr) If String.IsNullOrEmpty(mime) Then mime = "application/octet-stream" Return mime Catch e As Exception Return "application/octet-stream" End Try End Function Private Shared Sub InitializeMimeTypeLists() knownTypes = New List(Of String) knownTypes = New String() {"text/plain", _ "text/html", _ "text/xml", _ "text/richtext", _ "text/scriptlet", _ "audio/x-aiff", _ "audio/basic", _ "audio/mid", _ "audio/wav", _ "image/gif", _ "image/jpeg", _ "image/pjpeg", _ "image/png", _ "image/x-png", _ "image/tiff", _ "image/bmp", _ "image/x-xbitmap", _ "image/x-jg", _ "image/x-emf", _ "image/x-wmf", _ "video/avi", _ "video/mpeg", _ "application/octet-stream", _ "application/postscript", _ "application/base64", _ "application/macbinhex40", _ "application/pdf", _ "application/xml", _ "application/atom+xml", _ "application/rss+xml", _ "application/x-compressed", _ "application/x-zip-compressed", _ "application/x-gzip-compressed", _ "application/java", _ "application/x-msdownload" _ }.ToList MimeTypes = New Dictionary(Of String, String) With MimeTypes .Add("3dm", "x-world/x-3dmf") .Add("3dmf", "x-world/x-3dmf") .Add("a", "application/octet-stream") .Add("aab", "application/x-authorware-bin") .Add("aam", "application/x-authorware-map") .Add("aas", "application/x-authorware-seg") .Add("abc", "text/vnd.abc") .Add("acgi", "text/html") .Add("afl", "video/animaflex") .Add("ai", "application/postscript") .Add("aif", "audio/aiff") .Add("aifc", "audio/aiff") .Add("aiff", "audio/aiff") .Add("aim", "application/x-aim") .Add("aip", "text/x-audiosoft-intra") .Add("ani", "application/x-navi-animation") .Add("aos", "application/x-nokia-9000-communicator-add-on-software") .Add("aps", "application/mime") .Add("arc", "application/octet-stream") .Add("arj", "application/arj") .Add("art", "image/x-jg") .Add("asf", "video/x-ms-asf") .Add("asm", "text/x-asm") .Add("asp", "text/asp") .Add("asx", "application/x-mplayer2") .Add("au", "audio/basic") .Add("avi", "video/avi") .Add("avs", "video/avs-video") .Add("bcpio", "application/x-bcpio") .Add("bin", "application/octet-stream") .Add("bm", "image/bmp") .Add("bmp", "image/bmp") .Add("boo", "application/book") .Add("book", "application/book") .Add("boz", "application/x-bzip2") .Add("bsh", "application/x-bsh") .Add("bz", "application/x-bzip") .Add("bz2", "application/x-bzip2") .Add("c", "text/plain") .Add("c++", "text/plain") .Add("cat", "application/vnd.ms-pki.seccat") .Add("cc", "text/plain") .Add("ccad", "application/clariscad") .Add("cco", "application/x-cocoa") .Add("cdf", "application/cdf") .Add("cer", "application/pkix-cert") .Add("cha", "application/x-chat") .Add("chat", "application/x-chat") .Add("class", "application/java") .Add("com", "application/octet-stream") .Add("conf", "text/plain") .Add("cpio", "application/x-cpio") .Add("cpp", "text/x-c") .Add("cpt", "application/x-cpt") .Add("crl", "application/pkcs-crl") .Add("css", "text/css") .Add("csv", "application/vnd.ms-excel") .Add("def", "text/plain") .Add("der", "application/x-x509-ca-cert") .Add("dif", "video/x-dv") .Add("dir", "application/x-director") .Add("dl", "video/dl") .Add("doc", "application/msword") .Add("dot", "application/msword") .Add("dp", "application/commonground") .Add("drw", "application/drafting") .Add("dump", "application/octet-stream") .Add("dv", "video/x-dv") .Add("dvi", "application/x-dvi") .Add("dwf", "drawing/x-dwf (old)") .Add("dwg", "application/acad") .Add("dxf", "application/dxf") .Add("eps", "application/postscript") .Add("es", "application/x-esrehber") .Add("etx", "text/x-setext") .Add("evy", "application/envoy") .Add("exe", "application/octet-stream") .Add("f", "text/plain") .Add("f90", "text/x-fortran") .Add("fdf", "application/vnd.fdf") .Add("fif", "image/fif") .Add("fli", "video/fli") .Add("for", "text/x-fortran") .Add("fpx", "image/vnd.fpx") .Add("g", "text/plain") .Add("g3", "image/g3fax") .Add("gif", "image/gif") .Add("gl", "video/gl") .Add("gsd", "audio/x-gsm") .Add("gtar", "application/x-gtar") .Add("gz", "application/x-compressed") .Add("h", "text/plain") .Add("help", "application/x-helpfile") .Add("hgl", "application/vnd.hp-hpgl") .Add("hh", "text/plain") .Add("hlp", "application/x-winhelp") .Add("htc", "text/x-component") .Add("htm", "text/html") .Add("html", "text/html") .Add("htmls", "text/html") .Add("htt", "text/webviewhtml") .Add("htx", "text/html") .Add("ice", "x-conference/x-cooltalk") .Add("ico", "image/x-icon") .Add("idc", "text/plain") .Add("ief", "image/ief") .Add("iefs", "image/ief") .Add("iges", "application/iges") .Add("igs", "application/iges") .Add("ima", "application/x-ima") .Add("imap", "application/x-httpd-imap") .Add("inf", "application/inf") .Add("ins", "application/x-internett-signup") .Add("ip", "application/x-ip2") .Add("isu", "video/x-isvideo") .Add("it", "audio/it") .Add("iv", "application/x-inventor") .Add("ivr", "i-world/i-vrml") .Add("ivy", "application/x-livescreen") .Add("jam", "audio/x-jam") .Add("jav", "text/plain") .Add("java", "text/plain") .Add("jcm", "application/x-java-commerce") .Add("jfif", "image/jpeg") .Add("jfif-tbnl", "image/jpeg") .Add("jpe", "image/jpeg") .Add("jpeg", "image/jpeg") .Add("jpg", "image/jpeg") .Add("jps", "image/x-jps") .Add("js", "application/x-javascript") .Add("jut", "image/jutvision") .Add("kar", "audio/midi") .Add("ksh", "application/x-ksh") .Add("la", "audio/nspaudio") .Add("lam", "audio/x-liveaudio") .Add("latex", "application/x-latex") .Add("lha", "application/lha") .Add("lhx", "application/octet-stream") .Add("list", "text/plain") .Add("lma", "audio/nspaudio") .Add("log", "text/plain") .Add("lsp", "application/x-lisp") .Add("lst", "text/plain") .Add("lsx", "text/x-la-asf") .Add("ltx", "application/x-latex") .Add("lzh", "application/octet-stream") .Add("lzx", "application/lzx") .Add("m", "text/plain") .Add("m1v", "video/mpeg") .Add("m2a", "audio/mpeg") .Add("m2v", "video/mpeg") .Add("m3u", "audio/x-mpequrl") .Add("man", "application/x-troff-man") .Add("map", "application/x-navimap") .Add("mar", "text/plain") .Add("mbd", "application/mbedlet") .Add("mc$", "application/x-magic-cap-package-1.0") .Add("mcd", "application/mcad") .Add("mcf", "image/vasa") .Add("mcp", "application/netmc") .Add("me", "application/x-troff-me") .Add("mht", "message/rfc822") .Add("mhtml", "message/rfc822") .Add("mid", "audio/midi") .Add("midi", "audio/midi") .Add("mif", "application/x-frame") .Add("mime", "message/rfc822") .Add("mjf", "audio/x-vnd.audioexplosion.mjuicemediafile") .Add("mjpg", "video/x-motion-jpeg") .Add("mm", "application/base64") .Add("mme", "application/base64") .Add("mod", "audio/mod") .Add("moov", "video/quicktime") .Add("mov", "video/quicktime") .Add("movie", "video/x-sgi-movie") .Add("mp2", "audio/mpeg") .Add("mp3", "audio/mpeg3") .Add("mpa", "audio/mpeg") .Add("mpc", "application/x-project") .Add("mpe", "video/mpeg") .Add("mpeg", "video/mpeg") .Add("mpg", "video/mpeg") .Add("mpga", "audio/mpeg") .Add("mpp", "application/vnd.ms-project") .Add("mpt", "application/x-project") .Add("mpv", "application/x-project") .Add("mpx", "application/x-project") .Add("mrc", "application/marc") .Add("ms", "application/x-troff-ms") .Add("mv", "video/x-sgi-movie") .Add("my", "audio/make") .Add("mzz", "application/x-vnd.audioexplosion.mzz") .Add("nap", "image/naplps") .Add("naplps", "image/naplps") .Add("nc", "application/x-netcdf") .Add("ncm", "application/vnd.nokia.configuration-message") .Add("nif", "image/x-niff") .Add("niff", "image/x-niff") .Add("nix", "application/x-mix-transfer") .Add("nsc", "application/x-conference") .Add("nvd", "application/x-navidoc") .Add("o", "application/octet-stream") .Add("oda", "application/oda") .Add("omc", "application/x-omc") .Add("omcd", "application/x-omcdatamaker") .Add("omcr", "application/x-omcregerator") .Add("p", "text/x-pascal") .Add("p10", "application/pkcs10") .Add("p12", "application/pkcs-12") .Add("p7a", "application/x-pkcs7-signature") .Add("p7c", "application/pkcs7-mime") .Add("pas", "text/pascal") .Add("pbm", "image/x-portable-bitmap") .Add("pcl", "application/vnd.hp-pcl") .Add("pct", "image/x-pict") .Add("pcx", "image/x-pcx") .Add("pdf", "application/pdf") .Add("pfunk", "audio/make") .Add("pgm", "image/x-portable-graymap") .Add("pic", "image/pict") .Add("pict", "image/pict") .Add("pkg", "application/x-newton-compatible-pkg") .Add("pko", "application/vnd.ms-pki.pko") .Add("pl", "text/plain") .Add("plx", "application/x-pixclscript") .Add("pm", "image/x-xpixmap") .Add("png", "image/png") .Add("pnm", "application/x-portable-anymap") .Add("pot", "application/mspowerpoint") .Add("pov", "model/x-pov") .Add("ppa", "application/vnd.ms-powerpoint") .Add("ppm", "image/x-portable-pixmap") .Add("pps", "application/mspowerpoint") .Add("ppt", "application/mspowerpoint") .Add("ppz", "application/mspowerpoint") .Add("pre", "application/x-freelance") .Add("prt", "application/pro_eng") .Add("ps", "application/postscript") .Add("psd", "application/octet-stream") .Add("pvu", "paleovu/x-pv") .Add("pwz", "application/vnd.ms-powerpoint") .Add("py", "text/x-script.phyton") .Add("pyc", "applicaiton/x-bytecode.python") .Add("qcp", "audio/vnd.qcelp") .Add("qd3", "x-world/x-3dmf") .Add("qd3d", "x-world/x-3dmf") .Add("qif", "image/x-quicktime") .Add("qt", "video/quicktime") .Add("qtc", "video/x-qtc") .Add("qti", "image/x-quicktime") .Add("qtif", "image/x-quicktime") .Add("ra", "audio/x-pn-realaudio") .Add("ram", "audio/x-pn-realaudio") .Add("ras", "application/x-cmu-raster") .Add("rast", "image/cmu-raster") .Add("rexx", "text/x-script.rexx") .Add("rf", "image/vnd.rn-realflash") .Add("rgb", "image/x-rgb") .Add("rm", "application/vnd.rn-realmedia") .Add("rmi", "audio/mid") .Add("rmm", "audio/x-pn-realaudio") .Add("rmp", "audio/x-pn-realaudio") .Add("rng", "application/ringing-tones") .Add("rnx", "application/vnd.rn-realplayer") .Add("roff", "application/x-troff") .Add("rp", "image/vnd.rn-realpix") .Add("rpm", "audio/x-pn-realaudio-plugin") .Add("rt", "text/richtext") .Add("rtf", "text/richtext") .Add("rtx", "application/rtf") .Add("rv", "video/vnd.rn-realvideo") .Add("s", "text/x-asm") .Add("s3m", "audio/s3m") .Add("saveme", "application/octet-stream") .Add("sbk", "application/x-tbook") .Add("scm", "application/x-lotusscreencam") .Add("sdml", "text/plain") .Add("sdp", "application/sdp") .Add("sdr", "application/sounder") .Add("sea", "application/sea") .Add("set", "application/set") .Add("sgm", "text/sgml") .Add("sgml", "text/sgml") .Add("sh", "application/x-bsh") .Add("shtml", "text/html") .Add("sid", "audio/x-psid") .Add("sit", "application/x-sit") .Add("skd", "application/x-koan") .Add("skm", "application/x-koan") .Add("skp", "application/x-koan") .Add("skt", "application/x-koan") .Add("sl", "application/x-seelogo") .Add("smi", "application/smil") .Add("smil", "application/smil") .Add("snd", "audio/basic") .Add("sol", "application/solids") .Add("spc", "application/x-pkcs7-certificates") .Add("spl", "application/futuresplash") .Add("spr", "application/x-sprite") .Add("sprite", "application/x-sprite") .Add("src", "application/x-wais-source") .Add("ssi", "text/x-server-parsed-html") .Add("ssm", "application/streamingmedia") .Add("sst", "application/vnd.ms-pki.certstore") .Add("step", "application/step") .Add("stl", "application/sla") .Add("stp", "application/step") .Add("sv4cpio", "application/x-sv4cpio") .Add("sv4crc", "application/x-sv4crc") .Add("svf", "image/vnd.dwg") .Add("svr", "application/x-world") .Add("swf", "application/x-shockwave-flash") .Add("t", "application/x-troff") .Add("talk", "text/x-speech") .Add("tar", "application/x-tar") .Add("tbk", "application/toolbook") .Add("tcl", "application/x-tcl") .Add("tcsh", "text/x-script.tcsh") .Add("tex", "application/x-tex") .Add("texi", "application/x-texinfo") .Add("texinfo", "application/x-texinfo") .Add("text", "text/plain") .Add("tgz", "application/x-compressed") .Add("tif", "image/tiff") .Add("tr", "application/x-troff") .Add("tsi", "audio/tsp-audio") .Add("tsp", "audio/tsplayer") .Add("tsv", "text/tab-separated-values") .Add("turbot", "image/florian") .Add("txt", "text/plain") .Add("uil", "text/x-uil") .Add("uni", "text/uri-list") .Add("unis", "text/uri-list") .Add("unv", "application/i-deas") .Add("uri", "text/uri-list") .Add("uris", "text/uri-list") .Add("ustar", "application/x-ustar") .Add("uu", "application/octet-stream") .Add("vcd", "application/x-cdlink") .Add("vcs", "text/x-vcalendar") .Add("vda", "application/vda") .Add("vdo", "video/vdo") .Add("vew", "application/groupwise") .Add("viv", "video/vivo") .Add("vivo", "video/vivo") .Add("vmd", "application/vocaltec-media-desc") .Add("vmf", "application/vocaltec-media-file") .Add("voc", "audio/voc") .Add("vos", "video/vosaic") .Add("vox", "audio/voxware") .Add("vqe", "audio/x-twinvq-plugin") .Add("vqf", "audio/x-twinvq") .Add("vql", "audio/x-twinvq-plugin") .Add("vrml", "application/x-vrml") .Add("vrt", "x-world/x-vrt") .Add("vsd", "application/x-visio") .Add("vst", "application/x-visio") .Add("vsw", "application/x-visio") .Add("w60", "application/wordperfect6.0") .Add("w61", "application/wordperfect6.1") .Add("w6w", "application/msword") .Add("wav", "audio/wav") .Add("wb1", "application/x-qpro") .Add("wbmp", "image/vnd.wap.wbmp") .Add("web", "application/vnd.xara") .Add("wiz", "application/msword") .Add("wk1", "application/x-123") .Add("wmf", "windows/metafile") .Add("wml", "text/vnd.wap.wml") .Add("wmlc", "application/vnd.wap.wmlc") .Add("wmls", "text/vnd.wap.wmlscript") .Add("wmlsc", "application/vnd.wap.wmlscriptc") .Add("word", "application/msword") .Add("wp", "application/wordperfect") .Add("wp5", "application/wordperfect") .Add("wp6", "application/wordperfect") .Add("wpd", "application/wordperfect") .Add("wq1", "application/x-lotus") .Add("wri", "application/mswrite") .Add("wrl", "application/x-world") .Add("wrz", "model/vrml") .Add("wsc", "text/scriplet") .Add("wsrc", "application/x-wais-source") .Add("wtk", "application/x-wintalk") .Add("xbm", "image/x-xbitmap") .Add("xdr", "video/x-amt-demorun") .Add("xgz", "xgl/drawing") .Add("xif", "image/vnd.xiff") .Add("xl", "application/excel") .Add("xla", "application/excel") .Add("xlb", "application/excel") .Add("xlc", "application/excel") .Add("xld", "application/excel") .Add("xlk", "application/excel") .Add("xll", "application/excel") .Add("xlm", "application/excel") .Add("xls", "application/excel") .Add("xlt", "application/excel") .Add("xlv", "application/excel") .Add("xlw", "application/excel") .Add("xm", "audio/xm") .Add("xml", "text/xml") .Add("xmz", "xgl/movie") .Add("xpix", "application/x-vnd.ls-xpix") .Add("xpm", "image/x-xpixmap") .Add("x-png", "image/png") .Add("xsr", "video/x-amt-showrun") .Add("xwd", "image/x-xwd") .Add("xyz", "chemical/x-pdb") .Add("z", "application/x-compress") .Add("zip", "application/x-compressed") .Add("zoo", "application/octet-stream") .Add("zsh", "text/x-script.zsh") End With End Sub End Class
References
December 5, 2010 at 6:26 am
[…] File MIME type / Content Type « Pradeep1210's Blog […]
December 6, 2010 at 4:16 am
I love seeing websites that realize the value of providing a quality resource for free.
December 6, 2010 at 11:49 am
[…] File MIME type / Content Type « Pradeep1210's Blog […]
December 7, 2010 at 1:58 pm
nice post. thanks for sharing.
December 8, 2010 at 4:29 am
[…] File MIME type / Content Type « Pradeep1210's Blog […]
December 29, 2010 at 4:43 am
A truly beneficial publish by you my good friend. We have bookmarked this web page and can are available back again following several days to verify for almost any new posts that you just make.
December 31, 2010 at 5:14 pm
Hello ! I’m new on this forum, hope to talk to you soon 🙂
January 20, 2011 at 8:48 pm
Astounding article bro. This kind of is just a exceedingly nicely structured posting, just the tips I was hunting for. Thanks
February 4, 2011 at 11:37 am
This is genuinely a wonderful info. An document similar to this shows precisely how greatly the concept is actually appreciated by creator.
August 20, 2011 at 8:18 am
very helpful for us