The topics of HTML vs. XHTML and XHTML as text/html vs. XHTML as XHTML are quite complex. Unfortunately it's hard to get a complete picture, since information is spread mostly in bits and pieces around the web or is buried deep in W3C tech jargon. In addition there's some misinformation being circulated. I propose to make this the definitive SO resource about the topic, describing the most important aspects of:
- HTML 4
- HTML 5
- XHTML 1.0 as text/html, application/xml+xhtml
- XHTML 1.1 as application/xml+xhtml
What are the practical implications of each?
What are common pitfalls?
What is the importance of proper MIME types for each?
How do different browsers handle them?
What are common pitfalls?
What is the importance of proper MIME types for each?
How do different browsers handle them?
I'd like to see one answer per technology. I'm making this a community wiki, so rather than contributing redundant answers, please edit answers to complete the picture. Feel free to start with stubs. Also feel free to edit this question.
5 Answers
Contents.
- Terminology
- Languages and Serializations
- Specifications
- Browser Parsers and Content (MIME) Types
- Browser Support
- Validators and Document Type Definitions
- Quirks, Limited Quirks, and Standards modes.
Terminology
One of the difficulties of describing this is clearly that the terminology within the official specifications has changed over the years, since HTML was first introduced. What follows below is based on HTML5 terminology. Also, 'file' is used as a generic term to mean a file, document, input stream, octet stream, etc to avoid having to make fine distinctions.
Languages and Serializations
HTML and XHTML are defined in terms of a language and a serialization.
The language defines the vocabulary of the elements and attributes, and their content model, i.e. which elements are permitted inside which other elements, which attributes are allowed on which element, along with the purpose and meaning of each element and attribute.
The serialization defines how mark-up is used to describe these elements and attributes within a text document. This includes which tags are required and which can be inferred, and the rules for those inferences. It describes such things as how void elements should be marked up (e.g. “>” vs “/>”) and when attribute values need to be quoted.
Specifications
The HTML 4.01 specification is the current specification that defines both the HTML language and the HTML serialization.
The XML 1.0 specification defines a serialization but leaves the language to be defined by other specifications, which are termed “XML applications”
The XHTML 1.0 and 1.1 specifications are both in use. Essentially, they use the same language as HTML 4.01 but use a different serialization, one that is compatible with the XML 1.0 specification. i.e. XHTML is an XML application.
The HTML5 (as of 2010-04-18, draft) specification describes a new language for both HTML and XHTML. This language is mostly a superset of the HTML 4.01 language, but is intended to only be backward compatible with existing web tools, (e.g. browsers, search engines and authoring tools) and not with previous specifications, where differences arise. So the meaning of some elements are occasionally changed from the earlier specifications. Similarly, each of the serializations are backward compatible with the current tools.
Browser Parsers and Content (MIME) Types
When a text file is sent to a browser, it is parsed into its internal memory structure (object model). To do so it uses a parser which follows either the HTML serialization rules or XML serialization rules. Which parser it uses depends on what it deduces the content type to be, based for non-local files on the “content-type” HTTP header. Internally, once the file has been parsed, the browser treats the object model in almost the same way, regardless of whether it was originally supplied using an HTML or XHTML serialization.
For a browser to use its XHTML parser, the content type HTTP header must be one of the XML content types. Most commonly, this is either
application/xml
or application/xhtml+xml
. Any non XML content type will mean that the file, regardless of whether it meets all the XHTML language and serialization rules or not, will not be processed by the browser as XHTML.Using a HTTP content type of
text/html
(or in most fallback scenarios, where the content type is missing or any other non-XML type) will cause the browser to use its HTML serialization parser.One key difference between the two parsers is that the HTML serialization parser performs error recovery. If the input file to the parser does not meet the HTML serialization rules, the parser will recover in ways reverse engineered from previous browsers and carry on building its object model until it reaches the end of the file. HTML5 contains the first normative definition of the recovery but no mainstream browser has shipped an implementation of the algorithm enabled in a release version as of 2010-04-26.
In contrast, the XML serialization parser, will stop when it encounters anything that it cannot interpret as XML (i.e. when it discovers that the file is not XML well-formed). This is required of parsers by the XML 1.0 specification.
Browser Support
Most modern browsers contain support for both an HTML parser and an XML parser. However, in Microsoft Internet Explorer versions 8.0 and earlier, the XML parser cannot directly create an object model for rendering as an HTML page. The XML structure can, however be processed with an XSLT file to create a stream which in turn be parsed using the HTML parser to create a object model that can be rendered.
Starting with Internet Explorer 9 Platform Preview, XHTML supplied using an XML content type can be parsed directly in the same way as the other modern browsers.
When their XML parsers detect that their input files are not XML well-formed, some browsers display an error message, and others show the page as constructed up to the point where the error was detected and some offer the user the opportunity to have the file re-parsed using their HTML parser.
Validators and Document Type Definitions
HTML and XHTML files can begin with a Document Type Definition (DTD) declaration which indicates the language and serialization that is being used in the document. Validators, such as the one at http://validator.w3.org/ use this information to match the language and serialization used within the file against the rules defined in the DTD. It then reports errors based on where the rules in the DTD are violated by mark up in the file.
Not all HTML serialization and language rules can be described in a DTD, so validators only test for a subset of all the rules described by the specifications.
HTML 4.01 and XHTML 1.0 define Strict, Transitional, and Frameset DTDs which differ in the language elements and attributes that are permitted in compliant files.
Google play store apk download. This APK Installer can install Google Play Store APK file and obb asset into your Android devices for free. We can break the region restrictions to install any region Android.apk files into Android devices.
Validators based on HTML5 such as validator.nu behave more like browsers, processing the page according to the HTTP content type and using a non DTD-based rule set so that they catch errors that cannot be described by DTDs.
Quirks, Limited Quirks, and Standards modes.
Browsers do not validate the files sent to them. Nor do they use any DTD declaration to determine the language or serialization of the file. However, they do use it to guess the era in which the page was created, and therefore the likely parsing and rendering behaviour the author would have expected of a browser at that time. Accordingly, they define three parsing and rendering modes, known as Quirks mode, Limited Quirks (or Almost Standards) mode and Standards mode.
Any file served using an XML content type is always processed in standards mode. For files parsed using the HTML parser, if there is no DTD provided or the DTD is determined to be very old, browsers use their quirks mode. Broadly speaking, HTML 4.01 and XHTML files processed as text/html will be processed with limited quirks mode if they contain a transitional DTD and with standards mode if using a strict DTD.
Where the DTD is not recognised, the mode is determined by a complex set of rules. One special case is where the public and system identifiers are omitted and the declaration is simply <!DOCTYPE html>. This is known to be the shortest doctype declaration where current browsers will treat the file as standards mode. For that reason, it is the declaration specified to be used for HTML5 compliant files.
![Mime type for zip files Mime type for zip files](/uploads/1/2/3/7/123716513/555414779.jpg)
QA
XHTML has mature QA tools, but looser DTDs (e.g.
<textarea rows='
is a conformance error in HTML 4.01 and XHTML 1.0, but only a validity error in HTML 4.01*
). This is despite XHTML 1.0 being, theoretically, HTML 4.01 expressed as XML. There are numerous differences, which are not documented in the 'Differences with HTML 4' section of the spec.An XHTML document when served with a MIME type of application/xhtml+xml (see below) needs to conform 100% to XML standards, i.e. it needs to be 'well formed XML'. Even a single unescaped ampersand can cause the parser (the browser) to throw a warning and refuse to render the document. When creating dynamic XHTML sites which may include content supplied by third parties (e.g. any user input), great care needs to be taken to escape any and all invalid character sequences, to not allow invalid tags or attributes and to properly nest all elements.
Browser Support
- XHTML as text/html is well supported, but you have to jump through compatibility hoops. Without jumping through those hoops a perfectly valid page may fail to render (e.g.
<script type='text/javascript' src='foo' />
causing the rest of the document to be treated as a script instead of HTML) or display other issues. - XHTML as application/xhtml+xml is reasonably well supported by most browsers (minor bugs may exist). It does not work at all in Internet Explorer <= 8.
MIME type application/xhtml+xml vs text/html
XHTML served as text/html is neither XHTML nor HTML. It's handled like HTML by the browser, but since it's not HTML, it's treated as tag soup. Since Internet Explorer does not know how to handle XHTML using application/xhtml+xml, it will need to be served as text/html for IE only. Which means XHTML for IE is always tag soup, unless the differences between HTML and XHTML are tended to as well (see Differences with HTML 4).
Welcome to a world of pain. You get downstream proxy issues (you have to vary caching based on whatever request header you perform your conditional on). The document structure changes (e.g. tables without a
<tbody>
tag may or may not have a <tbody>
element depending on the content-type). It is a lot of work to produce, essentially, two almost identical documents.XHTML and Javascript
When an XHTML document is parsed with a proper application/xhtml+xml MIME type, there may be differences when manipulating DOM elements via Javascript. Some scripts which have not been prepared properly may work differently or fail in an XHTML environment.
Examples: under HTML, a JavaScript command like
And if you have
Examples: under HTML, a JavaScript command like
console.log(document.body.tagName);
would output 'BODY', whereas the same command under XHTML would output 'body'.And if you have
<table><tr>.</tr></table
in the markup, the table's firstChild
in JavaScript would be the tr
in XHTML, but a TBODY
in HTML.Advantages of using XHTML (as application/xhtml+xml)
- Allows direct interleaving of other XML formats like MathML and SVG.
- Is theoretically faster to parse. The difference is negligible in practice though.
*
Paragraph needs some polishing. QA
- HTML5 still has rather immature QA tools
- HTML 4 has been around a long time and has very mature QA tools
Browser Support
- HTML 5 — Bits and pieces are supported by various browsers. You need Javascript to support most things, basic structural elements (like
<section>
) fall over very badly if the Javascript isn't available.*
- HTML 4 is well supported
*
Some clarification and examples needed.Why?
HTML as well as XHTML comes in different flavors, namely
Strict
, Transitional
and Frameset
. Each 'dialect' specifies a different set of elements which are allowed to be used.Jumping in the deep end with
Strict
limits some of your options out of the box (e.g. not being able to specify target
Chief keef finally rich album mp3 download zip. attributes) that make it a no-go for many.Main Differences
Please expand
It’s really not that complex, and it really doesn’t matter very much.
- HTML and XHTML differ only in syntax.
- You can’t serve XHTML as XML to Internet Explorer, and you don’t get any benefits in other browsers by doing so.
What else do you need to know?
Html Mime Type Excel
Not the answer you're looking for? Browse other questions tagged htmlxhtmlhtml5mime-types or ask your own question.
Table of Contents
How can I get the MIME type from a file extension in C#? This is a rather common question among developers, an evergreen requirement that I happen to heard at least once a year from friends & colleagues working with ASP.NET MVC,ASP.NET Web API and (lately) .NET Core. The reason is pretty much obvious: whenever you end up working with file object storage in any web-based or client-based application, you will sooner or later have to retrieve the MIME type related to the byte array you’re dealing with.
After some initial misadventures, they fall in love. Movie Information Raj and Simran meet on a trip to Europe. Film dilwale dulhania le jayenge full watch. The battle begins to win over two traditional families. Dilwale Dulhania Le Jayenge Full Movie Watch Online, Watch Dilwale Dulhania Le Jayenge 1995 Online Full Movie Free DVDRip, Download and Watch Online Latest Hindi HD HDrip BluRay DVDscr 720P 1080p MP4 MKV Movies WatchFilmy.
There are a number of ways/techniques to do that, but – for the sake of simplicity – we will put them down to two: looking them up within the Windows Registry or relying to static, hard-coded MIME type lists. We won’t consider anything that involves querying an external service, as we do want an efficient way to deal with such issue.
Using the Windows Registry
The main benefit of this technique is the simplicity: you will get the job done in few lines of code.
2 4 6 8 10 12 14 | /// Retrieves the MimeType bound to the given filename or extension by looking into the Windows Registry entries. /// NOTE: This method supports only the MimeTypes registered in the server OS / Windows installation. /// <param name='fileNameOrExtension'>a valid filename (file.txt) or extension (.txt or txt)</param> /// <returns>A valid Mime Type (es. text/plain)</returns> publicstaticstringGetMimeTypeByWindowsRegistry(stringfileNameOrExtension) stringmimeType='application/unknown'; stringext=(fileNameOrExtension.Contains('.'))?System.IO.Path.GetExtension(fileNameOrExtension).ToLower():'.'+fileNameOrExtension; Microsoft.Win32.RegistryKey regKey=Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); if(regKey!=null&®Key.GetValue('Content Type')!=null)mimeType=regKey.GetValue('Content Type').ToString(); } |
However, there is a major caveat: your look-up process will be limited to those MIME types registered within the Windows OS installed on the machine hosting your application. Meaning that, as long as you web server doesn’t support – for example – PDFs, you won’t get the MIME type for PDF files. This is undoubtely a major issue that might leads some to prefer the following method instead.
Using a static MIME Type Map
To overcome the big limitation of the previous method you can use a manually-built static MIME map containing the MIME types for the most common / used / popular file extensions. There are many of them available throughout the web: I used to have mine too, until I found this great GitHub project that covers a gigantic amount of them: it also includes an efficient and deterministic two-way mapping, so you can also use it the other way around (retrieve the extension from a given MIME type).
Here’s the source code updated up to 31 Jan, 2017:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 280 282 284 286 288 290 292 294 296 298 300 302 304 306 308 310 312 314 316 318 320 322 324 326 328 330 332 334 336 338 340 342 344 346 348 350 352 354 356 358 360 362 364 366 368 370 372 374 376 378 380 382 384 386 388 390 392 394 396 398 400 402 404 406 408 410 412 414 416 418 420 422 424 426 428 430 432 434 436 438 440 442 444 446 448 450 452 454 456 458 460 462 464 466 468 470 472 474 476 478 480 482 484 486 488 490 492 494 496 498 500 502 504 506 508 510 512 514 516 518 520 522 524 526 528 530 532 534 536 538 540 542 544 546 548 550 552 554 556 558 560 562 564 566 568 570 572 574 576 578 580 582 584 586 588 590 592 594 596 598 600 602 604 606 608 610 612 614 616 618 620 622 624 626 628 630 632 634 636 638 640 642 644 646 648 650 652 654 656 658 660 662 664 666 668 670 672 674 676 678 680 682 684 686 688 690 692 694 696 698 700 702 704 706 708 710 712 714 716 718 720 722 724 726 728 730 732 734 736 | usingSystem.Collections.Generic; { { privatestaticreadonlyLazy<IDictionary<string,string>>_mappings=newLazy<IDictionary<string,string>>(BuildMappings); privatestaticIDictionary<string,string>BuildMappings() varmappings=newDictionary<string,string>(StringComparer.OrdinalIgnoreCase){ #region Big freaking list of mime types // maps both ways, // and // // any mime types on left side not pre-loaded on right side, are added automatically // some mime types can map to multiple extensions, so to get a deterministic mapping, // // combination of values from Windows 7 Registry and // from C:WindowsSystem32inetsrvconfigapplicationHost.config // // Some added based on http://www.iana.org/assignments/media-types/media-types.xhtml // {'.3g2','video/3gpp2'}, {'.3gp2','video/3gpp2'}, {'.7z','application/x-7z-compressed'}, {'.AAC','audio/aac'}, {'.aax','audio/vnd.audible.aax'}, {'.aca','application/octet-stream'}, {'.accdb','application/msaccess'}, {'.accde','application/msaccess'}, {'.accdt','application/msaccess'}, {'.accft','application/msaccess.ftemplate'}, {'.AddIn','text/xml'}, {'.adobebridge','application/x-bridge-url'}, {'.ADT','audio/vnd.dlna.adts'}, {'.afm','application/octet-stream'}, {'.aif','audio/aiff'}, {'.aiff','audio/aiff'}, {'.air','application/vnd.adobe.air-application-installer-package+zip'}, {'.anx','application/annodex'}, {'.apk','application/vnd.android.package-archive'}, {'.art','image/x-jg'}, {'.asax','application/xml'}, {'.asd','application/octet-stream'}, {'.ashx','application/xml'}, {'.asm','text/plain'}, {'.aspx','application/xml'}, {'.asx','video/x-ms-asf'}, {'.au','audio/basic'}, {'.axa','audio/annodex'}, {'.axv','video/annodex'}, {'.bcpio','application/x-bcpio'}, {'.bmp','image/bmp'}, {'.cab','application/octet-stream'}, {'.calx','application/vnd.ms-office.calx'}, {'.cc','text/plain'}, {'.cdda','audio/aiff'}, {'.cer','application/x-x509-ca-cert'}, {'.chm','application/octet-stream'}, {'.clp','application/x-msclip'}, {'.cmx','image/x-cmx'}, {'.cod','image/cis-cod'}, {'.contact','text/x-ms-contact'}, {'.cpio','application/x-cpio'}, {'.crd','application/x-mscardfile'}, {'.crt','application/x-x509-ca-cert'}, {'.csdproj','text/plain'}, {'.csproj','text/plain'}, {'.csv','text/csv'}, {'.cxx','text/plain'}, {'.datasource','application/xml'}, {'.dcr','application/x-director'}, {'.deploy','application/octet-stream'}, {'.dgml','application/xml'}, {'.dif','video/x-dv'}, {'.disco','text/xml'}, {'.dll','application/x-msdownload'}, {'.dlm','text/dlm'}, {'.docm','application/vnd.ms-word.document.macroEnabled.12'}, {'.docx','application/vnd.openxmlformats-officedocument.wordprocessingml.document'}, {'.dotm','application/vnd.ms-word.template.macroEnabled.12'}, {'.dotx','application/vnd.openxmlformats-officedocument.wordprocessingml.template'}, {'.dsw','text/plain'}, {'.dtsConfig','text/xml'}, {'.dvi','application/x-dvi'}, {'.dwp','application/octet-stream'}, {'.eml','message/rfc822'}, {'.eot','application/vnd.ms-fontobject'}, {'.etl','application/etl'}, {'.evy','application/envoy'}, {'.exe.config','text/xml'}, {'.fif','application/fractals'}, {'.fla','application/octet-stream'}, {'.flr','x-world/x-vrml'}, {'.fsscript','application/fsharp-script'}, {'.generictest','application/xml'}, {'.gpx','application/gpx+xml'}, {'.gsm','audio/x-gsm'}, {'.gz','application/x-gzip'}, {'.hdf','application/x-hdf'}, {'.hhc','application/x-oleobject'}, {'.hhp','application/octet-stream'}, {'.hpp','text/plain'}, {'.hta','application/hta'}, {'.htm','text/html'}, {'.htt','text/webviewhtml'}, {'.hxc','application/xml'}, {'.hxe','application/xml'}, {'.hxh','application/octet-stream'}, {'.hxk','application/xml'}, {'.hxr','application/octet-stream'}, {'.hxt','text/html'}, {'.hxw','application/octet-stream'}, {'.i','text/plain'}, {'.ics','application/octet-stream'}, {'.ief','image/ief'}, {'.inc','text/plain'}, {'.ini','text/plain'}, {'.ins','application/x-internet-signup'}, {'.ipg','application/x-itunes-ipg'}, {'.ipsw','application/x-itunes-ipsw'}, {'.isp','application/x-internet-signup'}, {'.itlp','application/x-itunes-itlp'}, {'.itpc','application/x-itunes-itpc'}, {'.jar','application/java-archive'}, {'.jck','application/liquidmotion'}, {'.jfif','image/pjpeg'}, {'.jpb','application/octet-stream'}, {'.jpeg','image/jpeg'}, {'.js','application/javascript'}, {'.jsx','text/jscript'}, {'.latex','application/x-latex'}, {'.library-ms','application/windows-library+xml'}, {'.loadtest','application/xml'}, {'.lsf','video/x-la-asf'}, {'.lsx','video/x-la-asf'}, {'.m13','application/x-msmediaview'}, {'.m1v','video/mpeg'}, {'.m2ts','video/vnd.dlna.mpeg-tts'}, {'.m3u','audio/x-mpegurl'}, {'.m4a','audio/m4a'}, {'.m4p','audio/m4p'}, {'.m4v','video/x-m4v'}, {'.mak','text/plain'}, {'.manifest','application/x-ms-manifest'}, {'.master','application/xml'}, {'.mdb','application/x-msaccess'}, {'.mdp','application/octet-stream'}, {'.mfp','application/x-shockwave-flash'}, {'.mhtml','message/rfc822'}, {'.midi','audio/mid'}, {'.mk','text/plain'}, {'.mno','text/xml'}, {'.mod','video/mpeg'}, {'.movie','video/x-sgi-movie'}, {'.mp2v','video/mpeg'}, {'.mp4','video/mp4'}, {'.mpa','video/mpeg'}, {'.mpeg','video/mpeg'}, {'.mpg','video/mpeg'}, {'.mpv2','video/mpeg'}, {'.ms','application/x-troff-ms'}, {'.mso','application/octet-stream'}, {'.mtx','application/xml'}, {'.mvc','application/x-miva-compiled'}, {'.nc','application/x-netcdf'}, {'.nws','message/rfc822'}, {'.oda','application/oda'}, {'.odb','application/vnd.oasis.opendocument.database'}, {'.odc','application/vnd.oasis.opendocument.chart'}, {'.odf','application/vnd.oasis.opendocument.formula'}, {'.odg','application/vnd.oasis.opendocument.graphics'}, {'.odi','application/vnd.oasis.opendocument.image'}, {'.odm','application/vnd.oasis.opendocument.text-master'}, {'.odp','application/vnd.oasis.opendocument.presentation'}, {'.ods','application/vnd.oasis.opendocument.spreadsheet'}, {'.odt','application/vnd.oasis.opendocument.text'}, {'.ogg','audio/ogg'}, {'.ogx','application/ogg'}, {'.onea','application/onenote'}, {'.onetmp','application/onenote'}, {'.onetoc2','application/onenote'}, {'.orderedtest','application/xml'}, {'.osdx','application/opensearchdescription+xml'}, {'.otg','application/vnd.oasis.opendocument.graphics-template'}, {'.oth','application/vnd.oasis.opendocument.text-web'}, {'.otp','application/vnd.oasis.opendocument.presentation-template'}, {'.ots','application/vnd.oasis.opendocument.spreadsheet-template'}, {'.ott','application/vnd.oasis.opendocument.text-template'}, {'.oxt','application/vnd.openofficeorg.extension'}, {'.p12','application/x-pkcs12'}, {'.p7c','application/pkcs7-mime'}, {'.p7r','application/x-pkcs7-certreqresp'}, {'.pbm','image/x-portable-bitmap'}, {'.pct','image/pict'}, {'.pcz','application/octet-stream'}, {'.pfb','application/octet-stream'}, {'.pfx','application/x-pkcs12'}, {'.pic','image/pict'}, {'.pkgdef','text/plain'}, {'.pko','application/vnd.ms-pki.pko'}, {'.pma','application/x-perfmon'}, {'.pml','application/x-perfmon'}, {'.pmw','application/x-perfmon'}, {'.pnm','image/x-portable-anymap'}, {'.pntg','image/x-macpaint'}, {'.pot','application/vnd.ms-powerpoint'}, {'.potm','application/vnd.ms-powerpoint.template.macroEnabled.12'}, {'.potx','application/vnd.openxmlformats-officedocument.presentationml.template'}, {'.ppam','application/vnd.ms-powerpoint.addin.macroEnabled.12'}, {'.pps','application/vnd.ms-powerpoint'}, {'.ppsm','application/vnd.ms-powerpoint.slideshow.macroEnabled.12'}, {'.ppsx','application/vnd.openxmlformats-officedocument.presentationml.slideshow'}, {'.pptm','application/vnd.ms-powerpoint.presentation.macroEnabled.12'}, {'.pptx','application/vnd.openxmlformats-officedocument.presentationml.presentation'}, {'.prm','application/octet-stream'}, {'.ps','application/postscript'}, {'.psd','application/octet-stream'}, {'.psm','application/octet-stream'}, {'.pub','application/x-mspublisher'}, {'.qht','text/x-html-insertion'}, {'.qt','video/quicktime'}, {'.qtif','image/x-quicktime'}, {'.qxd','application/octet-stream'}, {'.ram','audio/x-pn-realaudio'}, {'.ras','image/x-cmu-raster'}, {'.rc','text/plain'}, {'.rct','text/plain'}, {'.reg','text/plain'}, {'.rf','image/vnd.rn-realflash'}, {'.rgs','text/plain'}, {'.rmi','audio/mid'}, {'.roff','application/x-troff'}, {'.rqy','text/x-ms-rqy'}, {'.rtx','text/richtext'}, {'.s','text/plain'}, {'.safariextz','application/x-safari-safariextz'}, {'.scr','text/plain'}, {'.sd2','audio/x-sd2'}, {'.sea','application/octet-stream'}, {'.searchConnector-ms','application/windows-search-connector+xml'}, {'.setreg','application/set-registration-initiation'}, {'.sgimb','application/x-sgimb'}, {'.sh','application/x-sh'}, {'.shtml','text/html'}, {'.sitemap','application/xml'}, {'.sldm','application/vnd.ms-powerpoint.slide.macroEnabled.12'}, {'.sldx','application/vnd.openxmlformats-officedocument.presentationml.slide'}, {'.sln','text/plain'}, {'.smd','audio/x-smd'}, {'.smx','audio/x-smd'}, {'.snd','audio/basic'}, {'.snp','application/octet-stream'}, {'.sor','text/plain'}, {'.spl','application/futuresplash'}, {'.src','application/x-wais-source'}, {'.SSISDeploymentManifest','text/xml'}, {'.sst','application/vnd.ms-pki.certstore'}, {'.sv4cpio','application/x-sv4cpio'}, {'.svc','application/xml'}, {'.swf','application/x-shockwave-flash'}, {'.stp','application/step'}, {'.tar','application/x-tar'}, {'.testrunconfig','application/xml'}, {'.tex','application/x-tex'}, {'.texinfo','application/x-texinfo'}, {'.thmx','application/vnd.ms-officetheme'}, {'.tif','image/tiff'}, {'.tlh','text/plain'}, {'.toc','application/octet-stream'}, {'.trm','application/x-msterminal'}, {'.ts','video/vnd.dlna.mpeg-tts'}, {'.ttf','application/font-sfnt'}, {'.txt','text/plain'}, {'.uls','text/iuls'}, {'.ustar','application/x-ustar'}, {'.vbdproj','text/plain'}, {'.vbproj','text/plain'}, {'.vcf','text/x-vcard'}, {'.vcs','text/plain'}, {'.vddproj','text/plain'}, {'.vdproj','text/plain'}, {'.vml','text/xml'}, {'.vsct','text/xml'}, {'.vsi','application/ms-vsi'}, {'.vsixlangpack','text/xml'}, {'.vsmdi','application/xml'}, {'.vss','application/vnd.visio'}, {'.vssettings','text/xml'}, {'.vst','application/vnd.visio'}, {'.vsto','application/x-ms-vsto'}, {'.vsx','application/vnd.visio'}, {'.wav','audio/wav'}, {'.wax','audio/x-ms-wax'}, {'.wbmp','image/vnd.wap.wbmp'}, {'.wdb','application/vnd.ms-works'}, {'.webarchive','application/x-safari-webarchive'}, {'.webp','image/webp'},/* https://en.wikipedia.org/wiki/WebP */ {'.wiq','application/xml'}, {'.wks','application/vnd.ms-works'}, {'.wlpginstall','application/x-wlpg-detect'}, {'.wm','video/x-ms-wm'}, {'.wmd','application/x-ms-wmd'}, {'.wml','text/vnd.wap.wml'}, {'.wmls','text/vnd.wap.wmlscript'}, {'.wmp','video/x-ms-wmp'}, {'.wmx','video/x-ms-wmx'}, {'.woff','application/font-woff'}, {'.wps','application/vnd.ms-works'}, {'.wrl','x-world/x-vrml'}, {'.wsc','text/scriptlet'}, {'.wvx','video/x-ms-wvx'}, {'.xaf','x-world/x-vrml'}, {'.xap','application/x-silverlight-app'}, {'.xbm','image/x-xbitmap'}, {'.xht','application/xhtml+xml'}, {'.xla','application/vnd.ms-excel'}, {'.xlam','application/vnd.ms-excel.addin.macroEnabled.12'}, {'.xld','application/vnd.ms-excel'}, {'.xll','application/vnd.ms-excel'}, {'.xls','application/vnd.ms-excel'}, {'.xlsb','application/vnd.ms-excel.sheet.binary.macroEnabled.12'}, {'.xlsm','application/vnd.ms-excel.sheet.macroEnabled.12'}, {'.xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}, {'.xltm','application/vnd.ms-excel.template.macroEnabled.12'}, {'.xltx','application/vnd.openxmlformats-officedocument.spreadsheetml.template'}, {'.xml','text/xml'}, {'.xof','x-world/x-vrml'}, {'.xpm','image/x-xpixmap'}, {'.xrm-ms','text/xml'}, {'.xsd','text/xml'}, {'.xsl','text/xml'}, {'.xsn','application/octet-stream'}, {'.xspf','application/xspf+xml'}, {'.xwd','image/x-xwindowdump'}, {'.zip','application/zip'}, {'application/fsharp-script','.fsx'}, {'application/msword','.doc'}, {'application/onenote','.one'}, {'application/step','.step'}, {'application/vnd.ms-powerpoint','.ppt'}, {'application/vnd.visio','.vsd'}, {'application/x-shockwave-flash','.swf'}, {'application/x-zip-compressed','.zip'}, {'application/xml','.xml'},// anomoly, .xml -> text/xml, but application/xml -> many thingss, but all are xml, so safest is .xml {'audio/aiff','.aiff'}, {'audio/mid','.midi'}, {'audio/x-m4a','.m4a'}, {'audio/x-pn-realaudio','.ra'}, {'image/bmp','.bmp'}, {'image/pict','.pic'}, {'image/tiff','.tiff'}, {'image/x-quicktime','.qti'}, {'text/html','.html'}, {'text/scriptlet','.wsc'}, {'video/3gpp','.3gp'}, {'video/mp4','.mp4'}, {'video/quicktime','.mov'}, {'video/x-dv','.dv'}, {'video/x-ms-asf','.asf'}, varcache=mappings.ToList();// need ToList() to avoid modifying while still enumerating foreach(varmapping incache) if(!mappings.ContainsKey(mapping.Value)) mappings.Add(mapping.Value,mapping.Key); } returnmappings; { { } if(!extension.StartsWith('.')) extension='.'+extension; return_mappings.Value.TryGetValue(extension,outmime)?mime:'application/octet-stream'; { { } if(mimeType.StartsWith('.')) thrownewArgumentException('Requested mime type is not valid: '+mimeType); if(_mappings.Value.TryGetValue(mimeType,outextension)) returnextension; thrownewArgumentException('Requested mime type is not registered: '+mimeType); } |
The only issue I have with this technique is that it requires a TON of static code: that’s most expected, since everything you’re going to need is hard-coded inside the class. This also means that you will have to manually update the class anytime a new MIME type if released – unless you don’t want to support it.
Luckily enough the author now also keeps an updated NuGet package that – if you’re willing to add the dependance to your project – will keeps everything up-to-date without having to worry about it anymore.
A big thanks to Samuel Neff for the great work!
UPDATE: a comment by Martino Bordin, which we would like to thank, points out that ASP.NET 4.5 Framework introduced a new public method to the System.Web namespace – System.Web.MimeMapping.GetMimeMapping – which finally exposes the System.Web.MimeMapping sealed dictionary, which contains a whopping list of 342 known MIME Types! If you’re using (or willing to use) the 4.5 Framework you can definitely use that native method without adding external dependencies self-made classes, yet if you aren’t… You can (almost) do the same thing with a small “hack” you can implement using the System.Reflections namespace: the credits for such technique go to this awesome post from David Haney, which happens to be the Engineering Manager of the StackOverflow web portal.
Here’s the source code of his MimeMappingStealer class, which basically exposes sealed dictionary within a public method using an Invoke:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 | /// Exposes the Mime Mapping method that Microsoft hid from us. publicstaticclassMimeMappingStealer // The get mime mapping method info privatestaticreadonlyMethodInfo _getMimeMappingMethod=null; /// <summary> /// </summary> { // Load hidden mime mapping class and method from System.Web varassembly=Assembly.GetAssembly(typeof(HttpApplication)); Type mimeMappingType=assembly.GetType('System.Web.MimeMapping'); _getMimeMappingMethod=mimeMappingType.GetMethod('GetMimeMapping', BindingFlags.Instance|BindingFlags.Static|BindingFlags.Public| BindingFlags.NonPublic|BindingFlags.FlattenHierarchy); /// Exposes the hidden Mime mapping method. /// <param name='fileName'>The file name.</param> publicstaticstringGetMimeMapping(stringfileName) return(string)_getMimeMappingMethod.Invoke(null/*static method*/,new[]{fileName}); } |
![Type Type](/uploads/1/2/3/7/123716513/993051805.jpg)
A big thank to Martino and David for saving the day!
A multipurpose internet mail extension, or MIME type, is an internet standard that describes the contents of internet files based on their natures and formats. This cataloging helps the browser open the file with the appropriate extension or plugin. Although the term includes the word “mail,” it is used for web pages, too.
MIME types contain two parts: a type and a sub-type.
- The type describes the categorization of MIME types that are linked to each other.
- In contrast, a subtype is unique to a specific file type that is part of the type.
Here is the MIME type for HTML:
MIME types are defined in HTML by the
attribute on links, objects, and script and style tags.
Distinguishing Among MIME Types
Following is a list of most MIME types, with their file extensions and the applications that use them. Many computers use file extensions to help identify file types. So, if you have a file with an odd extension on your website, you can look up the MIME type in this list.
Some types are prefixed with X, and some are prefixed with VND. These prefixes, respectively, refer to sub-types not listed with the Internet Assigned Numbers Authority or values specific to a vendor.
This HTML tutorial explains the basics of MIME Type with syntax and examples.
Description
A MIME type (or media type) is an identifier for file formats or format contents on the Internet. MIME stands for Multipurpose Internet Mail Extensions and all MIME types are officially maintained by the Internet Assigned Numbers Authority (IANA). MIME types are traditionally written in all lowercase, but MIME types are actually case in-sensitive.
HTML elements such as the <a>, <embed>, <link>, <object> and <style> tags will use a MIME type in the type attribute.
Syntax
The syntax for a MIME type is:
Html Mime Map
Parameters or Arguments
The type is a string value. Here is the list of currently registered types for MIME type:
type | Description |
---|---|
application | Represents any type of binary data |
audio | Represents any type of audio file |
example | Represents any type of media used for 'examples' |
font | Represents any type of font file |
image | Represents any type of image including animated gif's, but it does not include video files |
message | Represents any type of message file |
model | Represents any type of model file |
multipart | Represents documents that are broken into multiparts with different MIME types in each part |
text | Represents any type of text data that is human readable |
video | Represents any type of video file |
Html Mime Type Example
Example
Here are examples of some of the more commonly used MIME types: