[Reproduced please indicate the source] :Blog.csdn.net/huahao1989/…

Multipurpose Internet Mail Extended (MIME) types are a standardized way to represent the nature and format of a document. It is defined and standardized in IETF RFC 6838. The Internet Assigned Numbers Authority (IANA) is the official body responsible for keeping track of all official MIME types, and you can find the full list up to date on the Media Types page.

Browsers typically use MIME types, not file extensions, to determine how to process documents; It is therefore important that the server is set up correctly to attach the correct MIME type to the header of the response object.

grammar

General structure

type/subtype

The MIME structure is very simple; Consists of two strings of type and subtype separated by a slash (/). Spaces are not allowed. * Type * represents a separate category that can be subclassed. Subtype represents each subdivided type.

MIME types are case insensitive, but are traditionally written in lower case.

Independent type

text/plain text/html image/jpeg image/png audio/mpeg audio/ogg audio/* video/mp4 application/* application/json Application/javascript application/ecmascript application/octet – stream…

Independent types indicate the classification of files, which can be one of the following:

type describe A typical example
text Indicates that the file is plain text and theoretically human readable text/plain.text/html.text/css, text/javascript
image It indicates some kind of image. Videos are not included, but dynamic images (such as animated giFs) also use the image type image/gif.image/png.image/jpeg.image/bmp.image/webp.image/x-icon.image/vnd.microsoft.icon
audio Indicates some kind of audio file audio/midi.audio/mpeg, audio/webm, audio/ogg, audio/wav
video Indicates some kind of video file video/webm.video/ogg
application Indicates some kind of binary data application/octet-stream.application/pkcs12.application/vnd.mspowerpoint.application/xhtml+xml.application/xml.application/pdf

For text file types without a specific subtype, text/plain is used. Similarly, binaries have no specific or known subtype, that is, application/ OCtet-stream is used.

Multipart type

multipart/form-data multipart/byteranges

The Multipart type represents the type of file type for a subdomain, often corresponding to different MIME types. This is a representation of compound files. Multipart /form-data can be used to associate HTML Forms with POST methods, and multipart/ Byteranges uses 206 Partial Content to send a subset of the entire file. HTTP takes a special approach to complex files that can’t be processed: it sends information directly to the browser (you might create a “save as” window, but you don’t know how to display inline files).

Important MIME types
  • application/octet-stream

This is the default value for the application file. Meaning * unknown application files, * browsers generally do not automatically execute or ask for execution. Browsers treat such files as if they had set the HTTP header Content-disposition value as attachment. In a multipart/form-data reply message body, the Content-Disposition header can be used in a sub-part of the multipart message body to give information about its corresponding field. Each subpart is separated by a delimiter defined in the Content-Type. The message body itself has no practical meaning.

  • text/plain

Default value for text files. Even if it means an unknown text file, the browser thinks it can be displayed directly. Text /plain does not mean some kind of textual data. If the browser wants an explicit type of text file, the browser does not consider whether they match. For example, if you download a text/plain file through a link that indicates a CSS file was downloaded. If the information provided is text/plain, the browser will not recognize it as a valid CSS file. The CSS type must be text/ CSS.

  • text/css

Any CSS file to be parsed as CSS in a web page must specify MIME as Text/CSS. Typically, the server does not recognize the MIME type of a.css file, but sends it to the browser as TEXT /plain or application/octet-stream: in this case, most browsers do not recognize it as a CSS file and simply ignore it. In particular, be careful to provide the correct MIME type for your CSS files.

  • text/html

All HTML content should use this type. Other MIME types of XHTML, such as Application/XML + HTML, are now largely unused (HTML5 has unified these formats).

Note: You still need to use application/ XML or application/ XHTML + XML if You intend to make use of XML’s strict parsing rules, use [
] or elements from non ‑ HTML, non ‑ SVG or non ‑ MathML XML namespaces, Parsing semantics as text/ HTML’s parsing semantics are conditional with those of application/ XML.

  • JavaScript types

According to the MIME sniffing standard, the following are valid JavaScript MIME types

  • application/javascript
  • application/ecmascript
  • application/x-ecmascript
  • application/x-javascript
  • text/ecmascript
  • text/javascript
  • Text/javascript1.0
  • Text/javascript1.1
  • Text/javascript1.2
  • Text/javascript1.3
  • Text/javascript1.4
  • Text/javascript1.5
  • text/jscript
  • text/livescript
  • text/x-ecmascript
  • text/x-javascript

All text JavaScript types have been deprecated by RFC 4329.

Image type

Only a small number of image types are widely supported, web-safe, and ready to use in Web pages:

The MIME type Image type
image/gif GIF images (lossy compression aspect replaced by PNG)
image/jpeg JPEG image
image/png PNG images
image/svg+xml SVG images (Vector images)

The classification here is somewhat controversial, with some arguing that WebP (image/ WebP) should be added here, but each new image type increases the amount of code and introduces new security issues, so browser vendors are very careful about adding types.

Other types of images can be found in Web documents. For example, many browsers support ICONS of the icon type as Favicons or similar ICONS, and the browser supports ICO images in the MIME type image/ X-icon

Although image/vnd.microsoft.icon is registered with ANA, it is still not widely supported and image/ X-icon is used as a substitute.

Audio and video types

HTML does not clearly define the file types that are supported for

The most common video file formats in the Web environment are these file types:

The MIME type Audio or video type
audio/wave

audio/wav

audio/x-wav

audio/x-pn-wav
Audio streaming media files. PCM audio encoding (WAVE Codec “1”) is generally supported, with limited support (if available) for other decoders.
audio/webm WebM audio file format. Vorbis and Opus are its most commonly used decoders.
video/webm Audio and video files in WebM format. VP8 and VP9 are its most commonly used video decoders. Vorbis and Opus are its most commonly used audio decoders.
audio/ogg Audio files in OGG multimedia file format. Vorbis is the most commonly used audio decoder for this multimedia file format.
video/ogg Audio and video files in OGG multimedia file format. A common video decoder is Theora; The audio decoder is Vorbis.
application/ogg Audio and video files in OGG multimedia file format. A common video decoder is Theora; The audio decoder is Vorbis.
application/json application/json (MIME_type)

En.wikipedia.org/wiki/Media_…

www.iana.org/assignments…
multipart/form-data

Multipart /form-data can be used for HTML forms to send information from the browser to the server. As a multi-part document format, it consists of different parts separated by a boundary (a string beginning with a ‘–‘). Each part has its own entity, as well as its own HTTP request header, with Content-Disposition and Content-Type being used in the file upload world, the most commonly used (Content-Length is ignored because the boundary line is a delimiter).

Content-Type: multipart/form-data; boundary=aBoundaryString
(other headers associated with the multipart document as a whole)

--aBoundaryString
Content-Disposition: form-data; name="myFile"; filename="img.jpg"
Content-Type: image/jpeg

(data)
--aBoundaryString
Content-Disposition: form-data; name="myField"

(data)
--aBoundaryString
(more subparts)
--aBoundaryString--

Copy the code

The form looks like this:

<form action="http://localhost:8000/" method="post" enctype="multipart/form-data">
  <input type="text" name="myTextField">
  <input type="checkbox" name="myCheckBox">Check</input>
  <input type="file" name="myFile">
  <button>Send the file</button>
</form>
Copy the code

A request like this is sent:

POST/HTTP/1.1 Host: localhost:8000 User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; The rv: 50.0) Gecko / 20100101 Firefox 50.0 / Accept: text/HTML, application/XHTML + XML, application/XML. Q = 0.9 * / *; Q = 0.8 Accept - Language: en - US, en. Q =0.5 accept-encoding: gzip, deflate Connection: keep-alive upgrade-insecure -Requests: 1 Content-type: multipart/form-data; boundary=---------------------------8721656041911415653955004498 Content-Length: 465 -----------------------------8721656041911415653955004498 Content-Disposition: form-data; name="myTextField" Test -----------------------------8721656041911415653955004498 Content-Disposition: form-data; name="myCheckBox" on -----------------------------8721656041911415653955004498 Content-Disposition: form-data; name="myFile"; filename="test.txt" Content-Type: text/plain Simple file. -----------------------------8721656041911415653955004498--Copy the code
multipart/byteranges

Multipart/Byteranges is used to send partial response packets back to the browser. When sending status code 206 Partial Content, this MIME type is used to indicate that the file is made up of several parts, each with its own request scope. Like many other content-types, they use delimiters to define boundaries. Each different section has HTTP headers such as Content-Type to indicate the actual Type of the file, and content-range to indicate its Range.

HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: multipart/byteranges; boundary=3d6b6a416f9b5
Content-Length: 385

--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 100-200/1270

eta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="vieport" content
--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 300-400/1270

-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: "Open Sans", "Helvetica
--3d6b6a416f9b5--
Copy the code

The importance of setting the correct MIME type

Many Web servers use the default application/ OCtet-stream to send unknown types. For security reasons, the browser does not allow custom default actions for these resources, so users must store them locally for use. Common file types that cause server configuration errors are as follows:

  • RAR encoding file. Ideally, in this case, the actual encoding file type is set; This is usually not possible (either because of a type unknown to the server or because the file contains many other different file types). In this case the server will send application/ X-RAR-compressed as a MIME type and the user will not define it as a useful default operation.

  • Audio or video files. Only files with the MIME type correctly set can be recognized and played by

  • Proprietary file type. Special attention should be paid to proprietary files. Using application/ OCtet-stream as a special treatment is not allowed: browsers are not allowed to define default behavior for generic MIME types (such as “open in Word”)

MIME sniff

When a MIME type is missing or the client thinks the file has the wrong MIME type set, the browser may perform MIME sniffing by looking at the resource. Each browser performs different actions in different situations. Because there are security issues with this operation, some MIME types represent executable content and some are not. Browsers can block MIME sniffing by setting x-Content-type-options with the request header content-type.

Other methods for passing file types

MIME types are not the only way to convey information about document types:

  • Name suffixes are sometimes used, especially on Microsoft Windows systems. Not all operating systems consider these suffixes to be meaningful (especially Linux and Mac OS), and like external MIME types, there is no guarantee that they are correct.
  • Magic numbers. The syntax of different types of files allows file type inference by looking at the structure. For example, each GIF file begins with a PNG file with a hex value of 47 49 46 38 [GIF89] or 89 50 4E 47 [.png]. Not all types of files have magic numbers, so this is not 100% reliable.

other

  • Properly configuring server MIME types
  • Media formats supported by the HTML audio and video elements
  • www.iana.org/assignments…

Welcome to the “backend old bird” public account, the next will be a series of feature articles, including Java, Python, Linux, SpringBoot, SpringCloud, Dubbo, algorithms, technical team management, as well as a variety of brain maps and learning materials. NFC technology, search technology, crawler technology, recommendation technology, audio and video interactive live broadcast, as long as I have time to sort out and share, please look forward to, ready-made notes, brain maps and learning materials if you have requirements can also leave a message on the public account to obtain in advance. As I am in all the teams are basically in the role of breakthrough and pathfinding, made a lot of things, encountered many pits, solve a lot of problems, welcome everyone to add the public account into the group to exchange and learn together.

[Reproduced please indicate the source] :Blog.csdn.net/huahao1989/…