如何从文件扩展名中获得MIME类型?
当前回答
当然,为了将通用完整性、动态性和可移植性的优势与最小的依赖性结合起来
你将结合Samuel Neff的静态字典与特定平台的查询,例如由Bryan Denny。
例如,在Linux下,命令行工具“xdg-mime”可以完成后面的部分。
其他回答
我更新了一些剧本。以防有人需要
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>
IANA媒体类型
我希望微软能把他们的行业标准统一起来!对于其他人,有兴趣的人:
离散的类型
application: https://www.iana.org/assignments/media-types/media-types.xhtml#application audio: https://www.iana.org/assignments/media-types/media-types.xhtml#audio example: https://www.iana.org/assignments/media-types/media-types.xhtml#examples font: https://www.iana.org/assignments/media-types/media-types.xhtml#font image: https://www.iana.org/assignments/media-types/media-types.xhtml#image model: https://www.iana.org/assignments/media-types/media-types.xhtml#model text: https://www.iana.org/assignments/media-types/media-types.xhtml#text video: https://www.iana.org/assignments/media-types/media-types.xhtml#video
多部分类型
信息:https://www.iana.org/assignments/media-types/media-types.xhtml消息 多部分:https://www.iana.org/assignments/media-types/media-types.xhtml #多部分
我想推荐一个阅读:MIME类型(IANA媒体类型)的Mozilla页面感兴趣的人!它的信息量很大!
在代码方面,上面的每个链接都有一个.csv文件下载:https://www.iana.org/assignments/media-types/application.csv
正如这里已经指出的,可以使用Dictionary或ConcurrentDictionary下载并使用Key - Value对填充Dictionary。
我对这些mimetypes的理解,使用apache列表,下面的脚本将为您提供一个包含所有mimetypes的字典。
var mimeTypeListUrl = "http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types";
var webClient = new WebClient();
var rawData = webClient.DownloadString(mimeTypeListUrl).Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries);
var extensionToMimeType = new Dictionary<string, string>();
var mimeTypeToExtension = new Dictionary<string, string[]>();
foreach (var row in rawData)
{
if (row.StartsWith("#")) continue;
var rowData = row.Split(new[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
if (rowData.Length != 2) continue;
var extensions = rowData[1].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (!mimeTypeToExtension.ContainsKey(rowData[0]))
{
mimeTypeToExtension.Add(rowData[0], extensions);
}
foreach (var extension in extensions)
{
if (!extensionToMimeType.ContainsKey(extension))
{
extensionToMimeType.Add(extension, rowData[0]);
}
}
}
您可以使用Apache的httpd提供的表。将其映射到函数、字典、列表等应该是很简单的。
另外,正如这里所看到的,extension->mime类型不一定是函数。每个文件扩展名可能有多种常见的MIME类型,所以你应该看看你的应用程序的需求,看看你为什么关心MIME类型,你想用它们“做什么”等等。您可以使用文件扩展名来确定相同的行为吗?您是否也需要读取文件的前几个字节来确定其MIME类型?
**使用MediaTypeNames类——>样本:MediaTypeNames. application . pdf **