如何从文件扩展名中获得MIME类型?


当前回答

您可以使用Apache的httpd提供的表。将其映射到函数、字典、列表等应该是很简单的。

另外,正如这里所看到的,extension->mime类型不一定是函数。每个文件扩展名可能有多种常见的MIME类型,所以你应该看看你的应用程序的需求,看看你为什么关心MIME类型,你想用它们“做什么”等等。您可以使用文件扩展名来确定相同的行为吗?您是否也需要读取文件的前几个字节来确定其MIME类型?

其他回答

我喜欢Samuel Neff所做的工作,但不喜欢每次创建字典的想法和开销。

我把它重组成一个开关案例。是的,你不能迭代它,但在我的情况下,我只使用它来快速查找一个值。特别是因为它是在web服务中完成的,所以我最不希望的就是在应用程序准备其结构时产生大量开销。编译器会把这个转换成散列查找,所以会非常快。

public static string GetMimeType(string extension)
{
  if (extension == null)
    throw new ArgumentNullException("extension");

  if (extension.StartsWith("."))
    extension = extension.Substring(1);


  switch (extension.ToLower())
{
    #region Big freaking list of mime types
    case "323": return "text/h323";
    case "3g2": return "video/3gpp2";
    case "3gp": return "video/3gpp";
    case "3gp2": return "video/3gpp2";
    case "3gpp": return "video/3gpp";
    case "7z": return "application/x-7z-compressed";
    case "aa": return "audio/audible";
    case "aac": return "audio/aac";
    case "aaf": return "application/octet-stream";
    case "aax": return "audio/vnd.audible.aax";
    case "ac3": return "audio/ac3";
    case "aca": return "application/octet-stream";
    case "accda": return "application/msaccess.addin";
    case "accdb": return "application/msaccess";
    case "accdc": return "application/msaccess.cab";
    case "accde": return "application/msaccess";
    case "accdr": return "application/msaccess.runtime";
    case "accdt": return "application/msaccess";
    case "accdw": return "application/msaccess.webapplication";
    case "accft": return "application/msaccess.ftemplate";
    case "acx": return "application/internet-property-stream";
    case "addin": return "text/xml";
    case "ade": return "application/msaccess";
    case "adobebridge": return "application/x-bridge-url";
    case "adp": return "application/msaccess";
    case "adt": return "audio/vnd.dlna.adts";
    case "adts": return "audio/aac";
    case "afm": return "application/octet-stream";
    case "ai": return "application/postscript";
    case "aif": return "audio/x-aiff";
    case "aifc": return "audio/aiff";
    case "aiff": return "audio/aiff";
    case "air": return "application/vnd.adobe.air-application-installer-package+zip";
    case "amc": return "application/x-mpeg";
    case "application": return "application/x-ms-application";
    case "art": return "image/x-jg";
    case "asa": return "application/xml";
    case "asax": return "application/xml";
    case "ascx": return "application/xml";
    case "asd": return "application/octet-stream";
    case "asf": return "video/x-ms-asf";
    case "ashx": return "application/xml";
    case "asi": return "application/octet-stream";
    case "asm": return "text/plain";
    case "asmx": return "application/xml";
    case "aspx": return "application/xml";
    case "asr": return "video/x-ms-asf";
    case "asx": return "video/x-ms-asf";
    case "atom": return "application/atom+xml";
    case "au": return "audio/basic";
    case "avi": return "video/x-msvideo";
    case "axs": return "application/olescript";
    case "bas": return "text/plain";
    case "bcpio": return "application/x-bcpio";
    case "bin": return "application/octet-stream";
    case "bmp": return "image/bmp";
    case "c": return "text/plain";
    case "cab": return "application/octet-stream";
    case "caf": return "audio/x-caf";
    case "calx": return "application/vnd.ms-office.calx";
    case "cat": return "application/vnd.ms-pki.seccat";
    case "cc": return "text/plain";
    case "cd": return "text/plain";
    case "cdda": return "audio/aiff";
    case "cdf": return "application/x-cdf";
    case "cer": return "application/x-x509-ca-cert";
    case "chm": return "application/octet-stream";
    case "class": return "application/x-java-applet";
    case "clp": return "application/x-msclip";
    case "cmx": return "image/x-cmx";
    case "cnf": return "text/plain";
    case "cod": return "image/cis-cod";
    case "config": return "application/xml";
    case "contact": return "text/x-ms-contact";
    case "coverage": return "application/xml";
    case "cpio": return "application/x-cpio";
    case "cpp": return "text/plain";
    case "crd": return "application/x-mscardfile";
    case "crl": return "application/pkix-crl";
    case "crt": return "application/x-x509-ca-cert";
    case "cs": return "text/plain";
    case "csdproj": return "text/plain";
    case "csh": return "application/x-csh";
    case "csproj": return "text/plain";
    case "css": return "text/css";
    case "csv": return "text/csv";
    case "cur": return "application/octet-stream";
    case "cxx": return "text/plain";
    case "dat": return "application/octet-stream";
    case "datasource": return "application/xml";
    case "dbproj": return "text/plain";
    case "dcr": return "application/x-director";
    case "def": return "text/plain";
    case "deploy": return "application/octet-stream";
    case "der": return "application/x-x509-ca-cert";
    case "dgml": return "application/xml";
    case "dib": return "image/bmp";
    case "dif": return "video/x-dv";
    case "dir": return "application/x-director";
    case "disco": return "text/xml";
    case "dll": return "application/x-msdownload";
    case "dll.config": return "text/xml";
    case "dlm": return "text/dlm";
    case "doc": return "application/msword";
    case "docm": return "application/vnd.ms-word.document.macroenabled.12";
    case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    case "dot": return "application/msword";
    case "dotm": return "application/vnd.ms-word.template.macroenabled.12";
    case "dotx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
    case "dsp": return "application/octet-stream";
    case "dsw": return "text/plain";
    case "dtd": return "text/xml";
    case "dtsconfig": return "text/xml";
    case "dv": return "video/x-dv";
    case "dvi": return "application/x-dvi";
    case "dwf": return "drawing/x-dwf";
    case "dwp": return "application/octet-stream";
    case "dxr": return "application/x-director";
    case "eml": return "message/rfc822";
    case "emz": return "application/octet-stream";
    case "eot": return "application/octet-stream";
    case "eps": return "application/postscript";
    case "etl": return "application/etl";
    case "etx": return "text/x-setext";
    case "evy": return "application/envoy";
    case "exe": return "application/octet-stream";
    case "exe.config": return "text/xml";
    case "fdf": return "application/vnd.fdf";
    case "fif": return "application/fractals";
    case "filters": return "application/xml";
    case "fla": return "application/octet-stream";
    case "flr": return "x-world/x-vrml";
    case "flv": return "video/x-flv";
    case "fsscript": return "application/fsharp-script";
    case "fsx": return "application/fsharp-script";
    case "generictest": return "application/xml";
    case "gif": return "image/gif";
    case "group": return "text/x-ms-group";
    case "gsm": return "audio/x-gsm";
    case "gtar": return "application/x-gtar";
    case "gz": return "application/x-gzip";
    case "h": return "text/plain";
    case "hdf": return "application/x-hdf";
    case "hdml": return "text/x-hdml";
    case "hhc": return "application/x-oleobject";
    case "hhk": return "application/octet-stream";
    case "hhp": return "application/octet-stream";
    case "hlp": return "application/winhlp";
    case "hpp": return "text/plain";
    case "hqx": return "application/mac-binhex40";
    case "hta": return "application/hta";
    case "htc": return "text/x-component";
    case "htm": return "text/html";
    case "html": return "text/html";
    case "htt": return "text/webviewhtml";
    case "hxa": return "application/xml";
    case "hxc": return "application/xml";
    case "hxd": return "application/octet-stream";
    case "hxe": return "application/xml";
    case "hxf": return "application/xml";
    case "hxh": return "application/octet-stream";
    case "hxi": return "application/octet-stream";
    case "hxk": return "application/xml";
    case "hxq": return "application/octet-stream";
    case "hxr": return "application/octet-stream";
    case "hxs": return "application/octet-stream";
    case "hxt": return "text/html";
    case "hxv": return "application/xml";
    case "hxw": return "application/octet-stream";
    case "hxx": return "text/plain";
    case "i": return "text/plain";
    case "ico": return "image/x-icon";
    case "ics": return "application/octet-stream";
    case "idl": return "text/plain";
    case "ief": return "image/ief";
    case "iii": return "application/x-iphone";
    case "inc": return "text/plain";
    case "inf": return "application/octet-stream";
    case "inl": return "text/plain";
    case "ins": return "application/x-internet-signup";
    case "ipa": return "application/x-itunes-ipa";
    case "ipg": return "application/x-itunes-ipg";
    case "ipproj": return "text/plain";
    case "ipsw": return "application/x-itunes-ipsw";
    case "iqy": return "text/x-ms-iqy";
    case "isp": return "application/x-internet-signup";
    case "ite": return "application/x-itunes-ite";
    case "itlp": return "application/x-itunes-itlp";
    case "itms": return "application/x-itunes-itms";
    case "itpc": return "application/x-itunes-itpc";
    case "ivf": return "video/x-ivf";
    case "jar": return "application/java-archive";
    case "java": return "application/octet-stream";
    case "jck": return "application/liquidmotion";
    case "jcz": return "application/liquidmotion";
    case "jfif": return "image/pjpeg";
    case "jnlp": return "application/x-java-jnlp-file";
    case "jpb": return "application/octet-stream";
    case "jpe": return "image/jpeg";
    case "jpeg": return "image/jpeg";
    case "jpg": return "image/jpeg";
    case "js": return "application/x-javascript";
    case "jsx": return "text/jscript";
    case "jsxbin": return "text/plain";
    case "latex": return "application/x-latex";
    case "library-ms": return "application/windows-library+xml";
    case "lit": return "application/x-ms-reader";
    case "loadtest": return "application/xml";
    case "lpk": return "application/octet-stream";
    case "lsf": return "video/x-la-asf";
    case "lst": return "text/plain";
    case "lsx": return "video/x-la-asf";
    case "lzh": return "application/octet-stream";
    case "m13": return "application/x-msmediaview";
    case "m14": return "application/x-msmediaview";
    case "m1v": return "video/mpeg";
    case "m2t": return "video/vnd.dlna.mpeg-tts";
    case "m2ts": return "video/vnd.dlna.mpeg-tts";
    case "m2v": return "video/mpeg";
    case "m3u": return "audio/x-mpegurl";
    case "m3u8": return "audio/x-mpegurl";
    case "m4a": return "audio/m4a";
    case "m4b": return "audio/m4b";
    case "m4p": return "audio/m4p";
    case "m4r": return "audio/x-m4r";
    case "m4v": return "video/x-m4v";
    case "mac": return "image/x-macpaint";
    case "mak": return "text/plain";
    case "man": return "application/x-troff-man";
    case "manifest": return "application/x-ms-manifest";
    case "map": return "text/plain";
    case "master": return "application/xml";
    case "mda": return "application/msaccess";
    case "mdb": return "application/x-msaccess";
    case "mde": return "application/msaccess";
    case "mdp": return "application/octet-stream";
    case "me": return "application/x-troff-me";
    case "mfp": return "application/x-shockwave-flash";
    case "mht": return "message/rfc822";
    case "mhtml": return "message/rfc822";
    case "mid": return "audio/mid";
    case "midi": return "audio/mid";
    case "mix": return "application/octet-stream";
    case "mk": return "text/plain";
    case "mmf": return "application/x-smaf";
    case "mno": return "text/xml";
    case "mny": return "application/x-msmoney";
    case "mod": return "video/mpeg";
    case "mov": return "video/quicktime";
    case "movie": return "video/x-sgi-movie";
    case "mp2": return "video/mpeg";
    case "mp2v": return "video/mpeg";
    case "mp3": return "audio/mpeg";
    case "mp4": return "video/mp4";
    case "mp4v": return "video/mp4";
    case "mpa": return "video/mpeg";
    case "mpe": return "video/mpeg";
    case "mpeg": return "video/mpeg";
    case "mpf": return "application/vnd.ms-mediapackage";
    case "mpg": return "video/mpeg";
    case "mpp": return "application/vnd.ms-project";
    case "mpv2": return "video/mpeg";
    case "mqv": return "video/quicktime";
    case "ms": return "application/x-troff-ms";
    case "msi": return "application/octet-stream";
    case "mso": return "application/octet-stream";
    case "mts": return "video/vnd.dlna.mpeg-tts";
    case "mtx": return "application/xml";
    case "mvb": return "application/x-msmediaview";
    case "mvc": return "application/x-miva-compiled";
    case "mxp": return "application/x-mmxp";
    case "nc": return "application/x-netcdf";
    case "nsc": return "video/x-ms-asf";
    case "nws": return "message/rfc822";
    case "ocx": return "application/octet-stream";
    case "oda": return "application/oda";
    case "odc": return "text/x-ms-odc";
    case "odh": return "text/plain";
    case "odl": return "text/plain";
    case "odp": return "application/vnd.oasis.opendocument.presentation";
    case "ods": return "application/oleobject";
    case "odt": return "application/vnd.oasis.opendocument.text";
    case "one": return "application/onenote";
    case "onea": return "application/onenote";
    case "onepkg": return "application/onenote";
    case "onetmp": return "application/onenote";
    case "onetoc": return "application/onenote";
    case "onetoc2": return "application/onenote";
    case "orderedtest": return "application/xml";
    case "osdx": return "application/opensearchdescription+xml";
    case "p10": return "application/pkcs10";
    case "p12": return "application/x-pkcs12";
    case "p7b": return "application/x-pkcs7-certificates";
    case "p7c": return "application/pkcs7-mime";
    case "p7m": return "application/pkcs7-mime";
    case "p7r": return "application/x-pkcs7-certreqresp";
    case "p7s": return "application/pkcs7-signature";
    case "pbm": return "image/x-portable-bitmap";
    case "pcast": return "application/x-podcast";
    case "pct": return "image/pict";
    case "pcx": return "application/octet-stream";
    case "pcz": return "application/octet-stream";
    case "pdf": return "application/pdf";
    case "pfb": return "application/octet-stream";
    case "pfm": return "application/octet-stream";
    case "pfx": return "application/x-pkcs12";
    case "pgm": return "image/x-portable-graymap";
    case "pic": return "image/pict";
    case "pict": return "image/pict";
    case "pkgdef": return "text/plain";
    case "pkgundef": return "text/plain";
    case "pko": return "application/vnd.ms-pki.pko";
    case "pls": return "audio/scpls";
    case "pma": return "application/x-perfmon";
    case "pmc": return "application/x-perfmon";
    case "pml": return "application/x-perfmon";
    case "pmr": return "application/x-perfmon";
    case "pmw": return "application/x-perfmon";
    case "png": return "image/png";
    case "pnm": return "image/x-portable-anymap";
    case "pnt": return "image/x-macpaint";
    case "pntg": return "image/x-macpaint";
    case "pnz": return "image/png";
    case "pot": return "application/vnd.ms-powerpoint";
    case "potm": return "application/vnd.ms-powerpoint.template.macroenabled.12";
    case "potx": return "application/vnd.openxmlformats-officedocument.presentationml.template";
    case "ppa": return "application/vnd.ms-powerpoint";
    case "ppam": return "application/vnd.ms-powerpoint.addin.macroenabled.12";
    case "ppm": return "image/x-portable-pixmap";
    case "pps": return "application/vnd.ms-powerpoint";
    case "ppsm": return "application/vnd.ms-powerpoint.slideshow.macroenabled.12";
    case "ppsx": return "application/vnd.openxmlformats-officedocument.presentationml.slideshow";
    case "ppt": return "application/vnd.ms-powerpoint";
    case "pptm": return "application/vnd.ms-powerpoint.presentation.macroenabled.12";
    case "pptx": return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    case "prf": return "application/pics-rules";
    case "prm": return "application/octet-stream";
    case "prx": return "application/octet-stream";
    case "ps": return "application/postscript";
    case "psc1": return "application/powershell";
    case "psd": return "application/octet-stream";
    case "psess": return "application/xml";
    case "psm": return "application/octet-stream";
    case "psp": return "application/octet-stream";
    case "pub": return "application/x-mspublisher";
    case "pwz": return "application/vnd.ms-powerpoint";
    case "qht": return "text/x-html-insertion";
    case "qhtm": return "text/x-html-insertion";
    case "qt": return "video/quicktime";
    case "qti": return "image/x-quicktime";
    case "qtif": return "image/x-quicktime";
    case "qtl": return "application/x-quicktimeplayer";
    case "qxd": return "application/octet-stream";
    case "ra": return "audio/x-pn-realaudio";
    case "ram": return "audio/x-pn-realaudio";
    case "rar": return "application/octet-stream";
    case "ras": return "image/x-cmu-raster";
    case "rat": return "application/rat-file";
    case "rc": return "text/plain";
    case "rc2": return "text/plain";
    case "rct": return "text/plain";
    case "rdlc": return "application/xml";
    case "resx": return "application/xml";
    case "rf": return "image/vnd.rn-realflash";
    case "rgb": return "image/x-rgb";
    case "rgs": return "text/plain";
    case "rm": return "application/vnd.rn-realmedia";
    case "rmi": return "audio/mid";
    case "rmp": return "application/vnd.rn-rn_music_package";
    case "roff": return "application/x-troff";
    case "rpm": return "audio/x-pn-realaudio-plugin";
    case "rqy": return "text/x-ms-rqy";
    case "rtf": return "application/rtf";
    case "rtx": return "text/richtext";
    case "ruleset": return "application/xml";
    case "s": return "text/plain";
    case "safariextz": return "application/x-safari-safariextz";
    case "scd": return "application/x-msschedule";
    case "sct": return "text/scriptlet";
    case "sd2": return "audio/x-sd2";
    case "sdp": return "application/sdp";
    case "sea": return "application/octet-stream";
    case "searchconnector-ms": return "application/windows-search-connector+xml";
    case "setpay": return "application/set-payment-initiation";
    case "setreg": return "application/set-registration-initiation";
    case "settings": return "application/xml";
    case "sgimb": return "application/x-sgimb";
    case "sgml": return "text/sgml";
    case "sh": return "application/x-sh";
    case "shar": return "application/x-shar";
    case "shtml": return "text/html";
    case "sit": return "application/x-stuffit";
    case "sitemap": return "application/xml";
    case "skin": return "application/xml";
    case "sldm": return "application/vnd.ms-powerpoint.slide.macroenabled.12";
    case "sldx": return "application/vnd.openxmlformats-officedocument.presentationml.slide";
    case "slk": return "application/vnd.ms-excel";
    case "sln": return "text/plain";
    case "slupkg-ms": return "application/x-ms-license";
    case "smd": return "audio/x-smd";
    case "smi": return "application/octet-stream";
    case "smx": return "audio/x-smd";
    case "smz": return "audio/x-smd";
    case "snd": return "audio/basic";
    case "snippet": return "application/xml";
    case "snp": return "application/octet-stream";
    case "sol": return "text/plain";
    case "sor": return "text/plain";
    case "spc": return "application/x-pkcs7-certificates";
    case "spl": return "application/futuresplash";
    case "src": return "application/x-wais-source";
    case "srf": return "text/plain";
    case "ssisdeploymentmanifest": return "text/xml";
    case "ssm": return "application/streamingmedia";
    case "sst": return "application/vnd.ms-pki.certstore";
    case "stl": return "application/vnd.ms-pki.stl";
    case "sv4cpio": return "application/x-sv4cpio";
    case "sv4crc": return "application/x-sv4crc";
    case "svc": return "application/xml";
    case "swf": return "application/x-shockwave-flash";
    case "t": return "application/x-troff";
    case "tar": return "application/x-tar";
    case "tcl": return "application/x-tcl";
    case "testrunconfig": return "application/xml";
    case "testsettings": return "application/xml";
    case "tex": return "application/x-tex";
    case "texi": return "application/x-texinfo";
    case "texinfo": return "application/x-texinfo";
    case "tgz": return "application/x-compressed";
    case "thmx": return "application/vnd.ms-officetheme";
    case "thn": return "application/octet-stream";
    case "tif": return "image/tiff";
    case "tiff": return "image/tiff";
    case "tlh": return "text/plain";
    case "tli": return "text/plain";
    case "toc": return "application/octet-stream";
    case "tr": return "application/x-troff";
    case "trm": return "application/x-msterminal";
    case "trx": return "application/xml";
    case "ts": return "video/vnd.dlna.mpeg-tts";
    case "tsv": return "text/tab-separated-values";
    case "ttf": return "application/octet-stream";
    case "tts": return "video/vnd.dlna.mpeg-tts";
    case "txt": return "text/plain";
    case "u32": return "application/octet-stream";
    case "uls": return "text/iuls";
    case "user": return "text/plain";
    case "ustar": return "application/x-ustar";
    case "vb": return "text/plain";
    case "vbdproj": return "text/plain";
    case "vbk": return "video/mpeg";
    case "vbproj": return "text/plain";
    case "vbs": return "text/vbscript";
    case "vcf": return "text/x-vcard";
    case "vcproj": return "application/xml";
    case "vcs": return "text/plain";
    case "vcxproj": return "application/xml";
    case "vddproj": return "text/plain";
    case "vdp": return "text/plain";
    case "vdproj": return "text/plain";
    case "vdx": return "application/vnd.ms-visio.viewer";
    case "vml": return "text/xml";
    case "vscontent": return "application/xml";
    case "vsct": return "text/xml";
    case "vsd": return "application/vnd.visio";
    case "vsi": return "application/ms-vsi";
    case "vsix": return "application/vsix";
    case "vsixlangpack": return "text/xml";
    case "vsixmanifest": return "text/xml";
    case "vsmdi": return "application/xml";
    case "vspscc": return "text/plain";
    case "vss": return "application/vnd.visio";
    case "vsscc": return "text/plain";
    case "vssettings": return "text/xml";
    case "vssscc": return "text/plain";
    case "vst": return "application/vnd.visio";
    case "vstemplate": return "text/xml";
    case "vsto": return "application/x-ms-vsto";
    case "vsw": return "application/vnd.visio";
    case "vsx": return "application/vnd.visio";
    case "vtx": return "application/vnd.visio";
    case "wav": return "audio/wav";
    case "wave": return "audio/wav";
    case "wax": return "audio/x-ms-wax";
    case "wbk": return "application/msword";
    case "wbmp": return "image/vnd.wap.wbmp";
    case "wcm": return "application/vnd.ms-works";
    case "wdb": return "application/vnd.ms-works";
    case "wdp": return "image/vnd.ms-photo";
    case "webarchive": return "application/x-safari-webarchive";
    case "webtest": return "application/xml";
    case "wiq": return "application/xml";
    case "wiz": return "application/msword";
    case "wks": return "application/vnd.ms-works";
    case "wlmp": return "application/wlmoviemaker";
    case "wlpginstall": return "application/x-wlpg-detect";
    case "wlpginstall3": return "application/x-wlpg3-detect";
    case "wm": return "video/x-ms-wm";
    case "wma": return "audio/x-ms-wma";
    case "wmd": return "application/x-ms-wmd";
    case "wmf": return "application/x-msmetafile";
    case "wml": return "text/vnd.wap.wml";
    case "wmlc": return "application/vnd.wap.wmlc";
    case "wmls": return "text/vnd.wap.wmlscript";
    case "wmlsc": return "application/vnd.wap.wmlscriptc";
    case "wmp": return "video/x-ms-wmp";
    case "wmv": return "video/x-ms-wmv";
    case "wmx": return "video/x-ms-wmx";
    case "wmz": return "application/x-ms-wmz";
    case "wpl": return "application/vnd.ms-wpl";
    case "wps": return "application/vnd.ms-works";
    case "wri": return "application/x-mswrite";
    case "wrl": return "x-world/x-vrml";
    case "wrz": return "x-world/x-vrml";
    case "wsc": return "text/scriptlet";
    case "wsdl": return "text/xml";
    case "wvx": return "video/x-ms-wvx";
    case "x": return "application/directx";
    case "xaf": return "x-world/x-vrml";
    case "xaml": return "application/xaml+xml";
    case "xap": return "application/x-silverlight-app";
    case "xbap": return "application/x-ms-xbap";
    case "xbm": return "image/x-xbitmap";
    case "xdr": return "text/plain";
    case "xht": return "application/xhtml+xml";
    case "xhtml": return "application/xhtml+xml";
    case "xla": return "application/vnd.ms-excel";
    case "xlam": return "application/vnd.ms-excel.addin.macroenabled.12";
    case "xlc": return "application/vnd.ms-excel";
    case "xld": return "application/vnd.ms-excel";
    case "xlk": return "application/vnd.ms-excel";
    case "xll": return "application/vnd.ms-excel";
    case "xlm": return "application/vnd.ms-excel";
    case "xls": return "application/vnd.ms-excel";
    case "xlsb": return "application/vnd.ms-excel.sheet.binary.macroenabled.12";
    case "xlsm": return "application/vnd.ms-excel.sheet.macroenabled.12";
    case "xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    case "xlt": return "application/vnd.ms-excel";
    case "xltm": return "application/vnd.ms-excel.template.macroenabled.12";
    case "xltx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.template";
    case "xlw": return "application/vnd.ms-excel";
    case "xml": return "text/xml";
    case "xmta": return "application/xml";
    case "xof": return "x-world/x-vrml";
    case "xoml": return "text/plain";
    case "xpm": return "image/x-xpixmap";
    case "xps": return "application/vnd.ms-xpsdocument";
    case "xrm-ms": return "text/xml";
    case "xsc": return "application/xml";
    case "xsd": return "text/xml";
    case "xsf": return "text/xml";
    case "xsl": return "text/xml";
    case "xslt": return "text/xml";
    case "xsn": return "application/octet-stream";
    case "xss": return "application/xml";
    case "xtp": return "application/octet-stream";
    case "xwd": return "image/x-xwindowdump";
    case "z": return "application/x-compress";
    case "zip": return "application/x-zip-compressed";
  #endregion
    default: return "application/octet-stream";
  }
}

当然,为了将通用完整性、动态性和可移植性的优势与最小的依赖性结合起来

你将结合Samuel Neff的静态字典与特定平台的查询,例如由Bryan Denny。

例如,在Linux下,命令行工具“xdg-mime”可以完成后面的部分。

为了使这篇文章更全面,对于。net核心开发人员有FileExtensionContentTypeProvider类,它涵盖了官方的MIME内容类型。

它在幕后工作——根据文件扩展名在Http响应头中设置ContentType。

如果您需要特殊的MIME类型,请参阅自定义MIME类型的示例:

public void Configure(IApplicationBuilder app)
{
    // Set up custom content types -associating file extension to MIME type
    var provider = new FileExtensionContentTypeProvider();
    // Add new mappings
    provider.Mappings[".myapp"] = "application/x-msdownload";
    provider.Mappings[".htm3"] = "text/html";
    provider.Mappings[".image"] = "image/png";
    // Replace an existing mapping
    provider.Mappings[".rtf"] = "application/x-msdownload";
    // Remove MP4 videos.
    provider.Mappings.Remove(".mp4");

    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "images")),
        RequestPath = new PathString("/MyImages"),
        ContentTypeProvider = provider
    });

    app.UseDirectoryBrowser(new DirectoryBrowserOptions()
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "images")),
        RequestPath = new PathString("/MyImages")
    });
}

我更新了一些剧本。以防有人需要

function getMime(){ let ext = fileUpload.value.split('.')[1]; let myOutput = getMimeType(ext); mimeType.value=myOutput; } function getMimeType(ext) { if (!ext.startsWith(".")) { ext = "." + ext; } let myList = [ {"key": ".323", "value": "text/h323"}, {"key": ".3g2", "value": "video/3gpp2"}, {"key": ".3gp", "value": "video/3gpp"}, {"key": ".3gp2", "value": "video/3gpp2"}, {"key": ".3gpp", "value": "video/3gpp"}, {"key": ".7z", "value": "application/x-7z-compressed"}, {"key": ".aa", "value": "audio/audible"}, {"key": ".AAC", "value": "audio/aac"}, {"key": ".aaf", "value": "application/octet-stream"}, {"key": ".aax", "value": "audio/vnd.audible.aax"}, {"key": ".ac3", "value": "audio/ac3"}, {"key": ".aca", "value": "application/octet-stream"}, {"key": ".accda", "value": "application/msaccess.addin"}, {"key": ".accdb", "value": "application/msaccess"}, {"key": ".accdc", "value": "application/msaccess.cab"}, {"key": ".accde", "value": "application/msaccess"}, {"key": ".accdr", "value": "application/msaccess.runtime"}, {"key": ".accdt", "value": "application/msaccess"}, {"key": ".accdw", "value": "application/msaccess.webapplication"}, {"key": ".accft", "value": "application/msaccess.ftemplate"}, {"key": ".acx", "value": "application/internet-property-stream"}, {"key": ".AddIn", "value": "text/xml"}, {"key": ".ade", "value": "application/msaccess"}, {"key": ".adobebridge", "value": "application/x-bridge-url"}, {"key": ".adp", "value": "application/msaccess"}, {"key": ".ADT", "value": "audio/vnd.dlna.adts"}, {"key": ".ADTS", "value": "audio/aac"}, {"key": ".afm", "value": "application/octet-stream"}, {"key": ".ai", "value": "application/postscript"}, {"key": ".aif", "value": "audio/x-aiff"}, {"key": ".aifc", "value": "audio/aiff"}, {"key": ".aiff", "value": "audio/aiff"}, {"key": ".air", "value": "application/vnd.adobe.air-application-installer-package+zip"}, {"key": ".amc", "value": "application/x-mpeg"}, {"key": ".application", "value": "application/x-ms-application"}, {"key": ".art", "value": "image/x-jg"}, {"key": ".asa", "value": "application/xml"}, {"key": ".asax", "value": "application/xml"}, {"key": ".ascx", "value": "application/xml"}, {"key": ".asd", "value": "application/octet-stream"}, {"key": ".asf", "value": "video/x-ms-asf"}, {"key": ".ashx", "value": "application/xml"}, {"key": ".asi", "value": "application/octet-stream"}, {"key": ".asm", "value": "text/plain"}, {"key": ".asmx", "value": "application/xml"}, {"key": ".aspx", "value": "application/xml"}, {"key": ".asr", "value": "video/x-ms-asf"}, {"key": ".asx", "value": "video/x-ms-asf"}, {"key": ".atom", "value": "application/atom+xml"}, {"key": ".au", "value": "audio/basic"}, {"key": ".avi", "value": "video/x-msvideo"}, {"key": ".axs", "value": "application/olescript"}, {"key": ".bas", "value": "text/plain"}, {"key": ".bcpio", "value": "application/x-bcpio"}, {"key": ".bin", "value": "application/octet-stream"}, {"key": ".bmp", "value": "image/bmp"}, {"key": ".c", "value": "text/plain"}, {"key": ".cab", "value": "application/octet-stream"}, {"key": ".caf", "value": "audio/x-caf"}, {"key": ".calx", "value": "application/vnd.ms-office.calx"}, {"key": ".cat", "value": "application/vnd.ms-pki.seccat"}, {"key": ".cc", "value": "text/plain"}, {"key": ".cd", "value": "text/plain"}, {"key": ".cdda", "value": "audio/aiff"}, {"key": ".cdf", "value": "application/x-cdf"}, {"key": ".cer", "value": "application/x-x509-ca-cert"}, {"key": ".chm", "value": "application/octet-stream"}, {"key": ".class", "value": "application/x-java-applet"}, {"key": ".clp", "value": "application/x-msclip"}, {"key": ".cmx", "value": "image/x-cmx"}, {"key": ".cnf", "value": "text/plain"}, {"key": ".cod", "value": "image/cis-cod"}, {"key": ".config", "value": "application/xml"}, {"key": ".contact", "value": "text/x-ms-contact"}, {"key": ".coverage", "value": "application/xml"}, {"key": ".cpio", "value": "application/x-cpio"}, {"key": ".cpp", "value": "text/plain"}, {"key": ".crd", "value": "application/x-mscardfile"}, {"key": ".crl", "value": "application/pkix-crl"}, {"key": ".crt", "value": "application/x-x509-ca-cert"}, {"key": ".cs", "value": "text/plain"}, {"key": ".csdproj", "value": "text/plain"}, {"key": ".csh", "value": "application/x-csh"}, {"key": ".csproj", "value": "text/plain"}, {"key": ".css", "value": "text/css"}, {"key": ".csv", "value": "text/csv"}, {"key": ".cur", "value": "application/octet-stream"}, {"key": ".cxx", "value": "text/plain"}, {"key": ".dat", "value": "application/octet-stream"}, {"key": ".datasource", "value": "application/xml"}, {"key": ".dbproj", "value": "text/plain"}, {"key": ".dcr", "value": "application/x-director"}, {"key": ".def", "value": "text/plain"}, {"key": ".deploy", "value": "application/octet-stream"}, {"key": ".der", "value": "application/x-x509-ca-cert"}, {"key": ".dgml", "value": "application/xml"}, {"key": ".dib", "value": "image/bmp"}, {"key": ".dif", "value": "video/x-dv"}, {"key": ".dir", "value": "application/x-director"}, {"key": ".disco", "value": "text/xml"}, {"key": ".dll", "value": "application/x-msdownload"}, {"key": ".dll.config", "value": "text/xml"}, {"key": ".dlm", "value": "text/dlm"}, {"key": ".doc", "value": "application/msword"}, {"key": ".docm", "value": "application/vnd.ms-word.document.macroEnabled.12"}, {"key": ".docx", "value": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, {"key": ".dot", "value": "application/msword"}, {"key": ".dotm", "value": "application/vnd.ms-word.template.macroEnabled.12"}, {"key": ".dotx", "value": "application/vnd.openxmlformats-officedocument.wordprocessingml.template"}, {"key": ".dsp", "value": "application/octet-stream"}, {"key": ".dsw", "value": "text/plain"}, {"key": ".dtd", "value": "text/xml"}, {"key": ".dtsConfig", "value": "text/xml"}, {"key": ".dv", "value": "video/x-dv"}, {"key": ".dvi", "value": "application/x-dvi"}, {"key": ".dwf", "value": "drawing/x-dwf"}, {"key": ".dwp", "value": "application/octet-stream"}, {"key": ".dxr", "value": "application/x-director"}, {"key": ".eml", "value": "message/rfc822"}, {"key": ".emz", "value": "application/octet-stream"}, {"key": ".eot", "value": "application/octet-stream"}, {"key": ".eps", "value": "application/postscript"}, {"key": ".etl", "value": "application/etl"}, {"key": ".etx", "value": "text/x-setext"}, {"key": ".evy", "value": "application/envoy"}, {"key": ".exe", "value": "application/octet-stream"}, {"key": ".exe.config", "value": "text/xml"}, {"key": ".fdf", "value": "application/vnd.fdf"}, {"key": ".fif", "value": "application/fractals"}, {"key": ".filters", "value": "Application/xml"}, {"key": ".fla", "value": "application/octet-stream"}, {"key": ".flr", "value": "x-world/x-vrml"}, {"key": ".flv", "value": "video/x-flv"}, {"key": ".fsscript", "value": "application/fsharp-script"}, {"key": ".fsx", "value": "application/fsharp-script"}, {"key": ".generictest", "value": "application/xml"}, {"key": ".gif", "value": "image/gif"}, {"key": ".group", "value": "text/x-ms-group"}, {"key": ".gsm", "value": "audio/x-gsm"}, {"key": ".gtar", "value": "application/x-gtar"}, {"key": ".gz", "value": "application/x-gzip"}, {"key": ".h", "value": "text/plain"}, {"key": ".hdf", "value": "application/x-hdf"}, {"key": ".hdml", "value": "text/x-hdml"}, {"key": ".hhc", "value": "application/x-oleobject"}, {"key": ".hhk", "value": "application/octet-stream"}, {"key": ".hhp", "value": "application/octet-stream"}, {"key": ".hlp", "value": "application/winhlp"}, {"key": ".hpp", "value": "text/plain"}, {"key": ".hqx", "value": "application/mac-binhex40"}, {"key": ".hta", "value": "application/hta"}, {"key": ".htc", "value": "text/x-component"}, {"key": ".htm", "value": "text/html"}, {"key": ".html", "value": "text/html"}, {"key": ".htt", "value": "text/webviewhtml"}, {"key": ".hxa", "value": "application/xml"}, {"key": ".hxc", "value": "application/xml"}, {"key": ".hxd", "value": "application/octet-stream"}, {"key": ".hxe", "value": "application/xml"}, {"key": ".hxf", "value": "application/xml"}, {"key": ".hxh", "value": "application/octet-stream"}, {"key": ".hxi", "value": "application/octet-stream"}, {"key": ".hxk", "value": "application/xml"}, {"key": ".hxq", "value": "application/octet-stream"}, {"key": ".hxr", "value": "application/octet-stream"}, {"key": ".hxs", "value": "application/octet-stream"}, {"key": ".hxt", "value": "text/html"}, {"key": ".hxv", "value": "application/xml"}, {"key": ".hxw", "value": "application/octet-stream"}, {"key": ".hxx", "value": "text/plain"}, {"key": ".i", "value": "text/plain"}, {"key": ".ico", "value": "image/x-icon"}, {"key": ".ics", "value": "application/octet-stream"}, {"key": ".idl", "value": "text/plain"}, {"key": ".ief", "value": "image/ief"}, {"key": ".iii", "value": "application/x-iphone"}, {"key": ".inc", "value": "text/plain"}, {"key": ".inf", "value": "application/octet-stream"}, {"key": ".inl", "value": "text/plain"}, {"key": ".ins", "value": "application/x-internet-signup"}, {"key": ".ipa", "value": "application/x-itunes-ipa"}, {"key": ".ipg", "value": "application/x-itunes-ipg"}, {"key": ".ipproj", "value": "text/plain"}, {"key": ".ipsw", "value": "application/x-itunes-ipsw"}, {"key": ".iqy", "value": "text/x-ms-iqy"}, {"key": ".isp", "value": "application/x-internet-signup"}, {"key": ".ite", "value": "application/x-itunes-ite"}, {"key": ".itlp", "value": "application/x-itunes-itlp"}, {"key": ".itms", "value": "application/x-itunes-itms"}, {"key": ".itpc", "value": "application/x-itunes-itpc"}, {"key": ".IVF", "value": "video/x-ivf"}, {"key": ".jar", "value": "application/java-archive"}, {"key": ".java", "value": "application/octet-stream"}, {"key": ".jck", "value": "application/liquidmotion"}, {"key": ".jcz", "value": "application/liquidmotion"}, {"key": ".jfif", "value": "image/pjpeg"}, {"key": ".jnlp", "value": "application/x-java-jnlp-file"}, {"key": ".jpb", "value": "application/octet-stream"}, {"key": ".jpe", "value": "image/jpeg"}, {"key": ".jpeg", "value": "image/jpeg"}, {"key": ".jpg", "value": "image/jpeg"}, {"key": ".js", "value": "application/x-javascript"}, {"key": ".json", "value": "application/json"}, {"key": ".jsx", "value": "text/jscript"}, {"key": ".jsxbin", "value": "text/plain"}, {"key": ".latex", "value": "application/x-latex"}, {"key": ".library-ms", "value": "application/windows-library+xml"}, {"key": ".lit", "value": "application/x-ms-reader"}, {"key": ".loadtest", "value": "application/xml"}, {"key": ".lpk", "value": "application/octet-stream"}, {"key": ".lsf", "value": "video/x-la-asf"}, {"key": ".lst", "value": "text/plain"}, {"key": ".lsx", "value": "video/x-la-asf"}, {"key": ".lzh", "value": "application/octet-stream"}, {"key": ".m13", "value": "application/x-msmediaview"}, {"key": ".m14", "value": "application/x-msmediaview"}, {"key": ".m1v", "value": "video/mpeg"}, {"key": ".m2t", "value": "video/vnd.dlna.mpeg-tts"}, {"key": ".m2ts", "value": "video/vnd.dlna.mpeg-tts"}, {"key": ".m2v", "value": "video/mpeg"}, {"key": ".m3u", "value": "audio/x-mpegurl"}, {"key": ".m3u8", "value": "audio/x-mpegurl"}, {"key": ".m4a", "value": "audio/m4a"}, {"key": ".m4b", "value": "audio/m4b"}, {"key": ".m4p", "value": "audio/m4p"}, {"key": ".m4r", "value": "audio/x-m4r"}, {"key": ".m4v", "value": "video/x-m4v"}, {"key": ".mac", "value": "image/x-macpaint"}, {"key": ".mak", "value": "text/plain"}, {"key": ".man", "value": "application/x-troff-man"}, {"key": ".manifest", "value": "application/x-ms-manifest"}, {"key": ".map", "value": "text/plain"}, {"key": ".master", "value": "application/xml"}, {"key": ".mda", "value": "application/msaccess"}, {"key": ".mdb", "value": "application/x-msaccess"}, {"key": ".mde", "value": "application/msaccess"}, {"key": ".mdp", "value": "application/octet-stream"}, {"key": ".me", "value": "application/x-troff-me"}, {"key": ".mfp", "value": "application/x-shockwave-flash"}, {"key": ".mht", "value": "message/rfc822"}, {"key": ".mhtml", "value": "message/rfc822"}, {"key": ".mid", "value": "audio/mid"}, {"key": ".midi", "value": "audio/mid"}, {"key": ".mix", "value": "application/octet-stream"}, {"key": ".mk", "value": "text/plain"}, {"key": ".mmf", "value": "application/x-smaf"}, {"key": ".mno", "value": "text/xml"}, {"key": ".mny", "value": "application/x-msmoney"}, {"key": ".mod", "value": "video/mpeg"}, {"key": ".mov", "value": "video/quicktime"}, {"key": ".movie", "value": "video/x-sgi-movie"}, {"key": ".mp2", "value": "video/mpeg"}, {"key": ".mp2v", "value": "video/mpeg"}, {"key": ".mp3", "value": "audio/mpeg"}, {"key": ".mp4", "value": "video/mp4"}, {"key": ".mp4v", "value": "video/mp4"}, {"key": ".mpa", "value": "video/mpeg"}, {"key": ".mpe", "value": "video/mpeg"}, {"key": ".mpeg", "value": "video/mpeg"}, {"key": ".mpf", "value": "application/vnd.ms-mediapackage"}, {"key": ".mpg", "value": "video/mpeg"}, {"key": ".mpp", "value": "application/vnd.ms-project"}, {"key": ".mpv2", "value": "video/mpeg"}, {"key": ".mqv", "value": "video/quicktime"}, {"key": ".ms", "value": "application/x-troff-ms"}, {"key": ".msi", "value": "application/octet-stream"}, {"key": ".mso", "value": "application/octet-stream"}, {"key": ".mts", "value": "video/vnd.dlna.mpeg-tts"}, {"key": ".mtx", "value": "application/xml"}, {"key": ".mvb", "value": "application/x-msmediaview"}, {"key": ".mvc", "value": "application/x-miva-compiled"}, {"key": ".mxp", "value": "application/x-mmxp"}, {"key": ".nc", "value": "application/x-netcdf"}, {"key": ".nsc", "value": "video/x-ms-asf"}, {"key": ".nws", "value": "message/rfc822"}, {"key": ".ocx", "value": "application/octet-stream"}, {"key": ".oda", "value": "application/oda"}, {"key": ".odc", "value": "text/x-ms-odc"}, {"key": ".odh", "value": "text/plain"}, {"key": ".odl", "value": "text/plain"}, {"key": ".odp", "value": "application/vnd.oasis.opendocument.presentation"}, {"key": ".ods", "value": "application/oleobject"}, {"key": ".odt", "value": "application/vnd.oasis.opendocument.text"}, {"key": ".webtest", "value": "application/xml"}, {"key": ".wiq", "value": "application/xml"}, {"key": ".wiz", "value": "application/msword"}, {"key": ".wks", "value": "application/vnd.ms-works"}, {"key": ".WLMP", "value": "application/wlmoviemaker"}, {"key": ".wlpginstall", "value": "application/x-wlpg-detect"}, {"key": ".wlpginstall3", "value": "application/x-wlpg3-detect"}, {"key": ".wm", "value": "video/x-ms-wm"}, {"key": ".wma", "value": "audio/x-ms-wma"}, {"key": ".wmd", "value": "application/x-ms-wmd"}, {"key": ".wmf", "value": "application/x-msmetafile"}, {"key": ".wml", "value": "text/vnd.wap.wml"}, {"key": ".wmlc", "value": "application/vnd.wap.wmlc"}, {"key": ".wmls", "value": "text/vnd.wap.wmlscript"}, {"key": ".wmlsc", "value": "application/vnd.wap.wmlscriptc"}, {"key": ".wmp", "value": "video/x-ms-wmp"}, {"key": ".wmv", "value": "video/x-ms-wmv"}, {"key": ".wmx", "value": "video/x-ms-wmx"}, {"key": ".wmz", "value": "application/x-ms-wmz"}, {"key": ".wpl", "value": "application/vnd.ms-wpl"}, {"key": ".wps", "value": "application/vnd.ms-works"}, {"key": ".wri", "value": "application/x-mswrite"}, {"key": ".wrl", "value": "x-world/x-vrml"}, {"key": ".wrz", "value": "x-world/x-vrml"}, {"key": ".wsc", "value": "text/scriptlet"}, {"key": ".wsdl", "value": "text/xml"}, {"key": ".wvx", "value": "video/x-ms-wvx"}, {"key": ".x", "value": "application/directx"}, {"key": ".xaf", "value": "x-world/x-vrml"}, {"key": ".xaml", "value": "application/xaml+xml"}, {"key": ".xap", "value": "application/x-silverlight-app"}, {"key": ".xbap", "value": "application/x-ms-xbap"}, {"key": ".xbm", "value": "image/x-xbitmap"}, {"key": ".xdr", "value": "text/plain"}, {"key": ".xht", "value": "application/xhtml+xml"}, {"key": ".xhtml", "value": "application/xhtml+xml"}, {"key": ".xla", "value": "application/vnd.ms-excel"}, {"key": ".xlam", "value": "application/vnd.ms-excel.addin.macroEnabled.12"}, {"key": ".xlc", "value": "application/vnd.ms-excel"}, {"key": ".xld", "value": "application/vnd.ms-excel"}, {"key": ".xlk", "value": "application/vnd.ms-excel"}, {"key": ".xll", "value": "application/vnd.ms-excel"}, {"key": ".xlm", "value": "application/vnd.ms-excel"}, {"key": ".xls", "value": "application/vnd.ms-excel"}, {"key": ".xlsb", "value": "application/vnd.ms-excel.sheet.binary.macroEnabled.12"}, {"key": ".xlsm", "value": "application/vnd.ms-excel.sheet.macroEnabled.12"}, {"key": ".xlsx", "value": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, {"key": ".xlt", "value": "application/vnd.ms-excel"}, {"key": ".xltm", "value": "application/vnd.ms-excel.template.macroEnabled.12"}, {"key": ".xltx", "value": "application/vnd.openxmlformats-officedocument.spreadsheetml.template"}, {"key": ".xlw", "value": "application/vnd.ms-excel"}, {"key": ".xml", "value": "text/xml"}, {"key": ".xmta", "value": "application/xml"}, {"key": ".xof", "value": "x-world/x-vrml"}, {"key": ".XOML", "value": "text/plain"}, {"key": ".xpm", "value": "image/x-xpixmap"}, {"key": ".xps", "value": "application/vnd.ms-xpsdocument"}, {"key": ".xrm-ms", "value": "text/xml"}, {"key": ".xsc", "value": "application/xml"}, {"key": ".xsd", "value": "text/xml"}, {"key": ".xsf", "value": "text/xml"}, {"key": ".xsl", "value": "text/xml"}, {"key": ".xslt", "value": "text/xml"}, {"key": ".xsn", "value": "application/octet-stream"}, {"key": ".xss", "value": "application/xml"}, {"key": ".xtp", "value": "application/octet-stream"}, {"key": ".xwd", "value": "image/x-xwindowdump"}, {"key": ".z", "value": "application/x-compress"}, {"key": ".zip", "value": "application/x-zip-compressed"} ] let index = myList.map((o) => o.key).indexOf(ext); let mime = myList[index]["value"]; return mime } <p><input id='fileUpload' type='file' name='fileUpload' onChange='getMime()'></p> <p>MimeType: <input id='mimeType' type='text' name='mimeType'></p>

. net框架提供了获取mime类型的内置方法。不需要编写自定义代码。

string mimeType = System.Web.MimeMapping.GetMimeMapping(fileName);