Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. HTTP
  3. Guides
  4. Media types

Media types (MIME types)

Amedia type (formerly known as aMultipurpose Internet Mail Extensions or MIME type) indicates the nature and format of a document, file, or assortment of bytes.MIME types are defined and standardized in IETF'sRFC 6838.

TheInternet Assigned Numbers Authority (IANA) is responsible for all official MIME types, and you can find the most up-to-date and complete list at theirMedia Types page.

Warning:Browsers use the MIME type,not the file extension, to determine how to process a URL,so it's important that web servers send the correct MIME type in the response'sContent-Type header.If this is not correctly configured, browsers are likely to misinterpret the contents of files, sites will not work correctly, and downloaded files may be mishandled.

Structure of a MIME type

A MIME type most commonly consists of just two parts: atype and asubtype, separated by a slash (/) — with no whitespace between:

type/subtype

Thetype represents the general category into which the data type falls, such asvideo ortext.

Thesubtype identifies the exact kind of data of the specified type the MIME type represents.For example, for the MIME typetext, the subtype might beplain (plain text),html (HTML source code), orcalendar (for iCalendar/.ics) files.

Each type has its own set of possible subtypes. A MIME type always has both a type and a subtype, never just one or the other.

An optionalparameter can be added to provide additional details:

type/subtype;parameter=value

For example, for any MIME type whose main type istext, you can add the optionalcharset parameter to specify the character set used for thecharacters in the data.If nocharset is specified, the default isASCII (US-ASCII) unless overridden by theuser agent's settings.To specify a UTF-8 text file, the MIME typetext/plain;charset=UTF-8 is used.

MIME types are case-insensitive but are traditionally written in lowercase. The parameter values can be case-sensitive.

Types

There are two classes of type:discrete andmultipart.Discrete types are types which represent a single file or medium, such as a single text or music file, or a single video.A multipart type represents a document that's comprised of multiple component parts, each of which may have its own individual MIME type; or, a multipart type may encapsulate multiple files being sent together in one transaction.For example, multipart MIME types are used when attaching multiple files to an email.

Discrete types

The discrete types currently registered with the IANA are:

application

Any kind of binary data that doesn't fall explicitly into one of the other types;either data that will be executed or interpreted in some way or binary data that requires a specific application or category of application to use.Generic binary data (or binary data whose true type is unknown) isapplication/octet-stream.Other common examples includeapplication/pdf,application/pkcs8, andapplication/zip.(See application type registry at IANA)

audio

Audio or music data. Examples includeaudio/mpeg,audio/vorbis.(See audio type registry at IANA)

example

Reserved for use as a placeholder in examples showing how to use MIME types.These should never be used outside of sample code listings and documentation.example can also be used as a subtype;for instance, in an example related to working with audio on the web, the MIME typeaudio/example can be used to indicate that the type is a placeholder and should be replaced with an appropriate one when using the code in the real world.

font

Font/typeface data. Common examples includefont/woff,font/ttf, andfont/otf.(See font type registry at IANA)

image

Image or graphical data including both bitmap and vector still images as well asanimated versions of still image formats such as animatedGIF or APNG.Common examples areimage/jpeg,image/png, andimage/svg+xml.(See image type registry at IANA)

model

Model data for a 3D object or scene. Examples includemodel/3mf andmodel/vrml.(See model type registry at IANA)

text

Text-only data including any human-readable content, source code, or textual data such as comma-separated value (CSV) formatted data.Examples include:text/plain,text/csv, andtext/html.(See text type registry at IANA)

video

Video data or files, such as MP4 movies (video/mp4).(See video type registry at IANA)

For text documents without a specific subtype,text/plain should be used.Similarly, for binary documents without a specific or known subtype,application/octet-stream should be used.

Multipart types

Multipart types indicate a category of document broken intopieces, often with different MIME types; they can also be used — especially in emailscenarios — to represent multiple, separate files which are all part of the sametransaction. They represent acomposite document.

Except formultipart/form-data, used in thePOST method ofHTML Forms, andmultipart/byteranges, used with206Partial Content to send part of a document, HTTP doesn't handle multipart documents in a special way: the message is transmitted to the browser (which will likelyshow a "Save As" window if it doesn't know how to display the document).

There are two multipart types:

message

A message that encapsulates other messages. This can be used, for instance, to represent an email that includes a forwarded message as part of its data,or to allow sending very large messages in chunks as if it were multiple messages.Examples includemessage/rfc822 (for forwarded or replied-to message quoting) andmessage/partial to allow breaking a large message into smaller ones automatically to be reassembled by the recipient.(See message type registry at IANA)

multipart

Data that consists of multiple components which may individually have different MIME types.Examples includemultipart/form-data (for data produced using theFormData API) andmultipart/byteranges (defined inRFC 7233, section 5.4.1 and used withHTTP's206"Partial Content" response returned when the fetched data is only part of the content, such as is delivered using theRange header).(See multipart type registry at IANA)

Important MIME types for Web developers

application/octet-stream

This is the default for binary files. As it meansunknown binary file, browsers usually don't execute it, or even ask if it should be executed. They treat it as if theContent-Disposition header was set toattachment, and propose a "Save As" dialog.

text/plain

This is the default for textual files. Even if it really means "unknown textual file," browsers assume they can display it.

Note:text/plain does not mean "any kind of textual data."If they expect a specific kind of textual data, they will likely not consider it a match.Specifically if they download atext/plain file from a<link> element declaring a CSS file, they will not recognize it as a valid CSS file if presented withtext/plain.The CSS mime typetext/css must be used.

text/css

CSS files used to style a Web pagemust be sent withtext/css.If a server doesn't recognize the.css suffix for CSS files, it may send them withtext/plain orapplication/octet-stream MIME types.If so, they won't be recognized as CSS by most browsers and will be ignored.

text/html

All HTML content should be served with this type. Alternative MIME types for XHTML (likeapplication/xhtml+xml) are mostly useless nowadays.

Note:Useapplication/xml orapplication/xhtml+xml if you want XML's strict parsing rules,<![CDATA[…]]> sections, or elements that aren't from HTML/SVG/MathML namespaces.

text/javascript

JavaScript content should always be served with the MIME typetext/javascript.For historic reasons, browsers may support somelegacy JavaScript types listed below, but you should not assume scripts served with any MIME type other thantext/javascript will always load or run.

Note that in HTML thetype attribute for<script> elements may only contain theJavaScript MIME type essence:text/javascript.Including any parameter, such ascharset=utf-8, is the same as setting thetype to any other MIME type: the script content is treated as a data block and is not executed as JavaScript.(Note that setting thetype to a JavaScript MIME type is a deprecated feature itself: you should omit thetype in this case.)In contrast, when using the HTTPContent-Type header you may optionally specify thecharset parameter as usual.

For more information see:IANA Media Types registry,RFC 9239, and theHTML specification.

Legacy JavaScript MIME types

In addition to thetext/javascript MIME type, for historical reasons, theMIME Sniffing Standard(the definition of how browsers should interpret MIME types and figureout what to do with content that doesn't have a valid one) also allows JavaScript to be served using any of the following legacy JavaScript MIME types:

Note:Even though any givenuser agent may support any or all of these, you should only usetext/javascript.It's the only MIME type guaranteed to work now and into the future.

application/json

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax.It is commonly used for transmitting data in web applications.

Image types

Files whose MIME type isimage contain image data.The subtype specifies which specific image file format the data represents.

The following image types are used commonly enough to be consideredsafe for use on web pages:

TheImage file type and format guide provides information and recommendations about when to use the different image formats.

Audio and video types

As is the case for images, HTML doesn't mandate that web browsers support any specific file and codec types for the<audio> and<video> elements, so it's important to consider your target audience and the range of browsers (and versions of those browsers) they may be using when choosing the file type and codecs to use for media.

Ourmedia container formats guide provides a list of the file types that are commonly supported by web browsers,including information about what their special use cases may be, any drawbacks they have, and compatibility information, along with other details.

Theaudio codec andvideo codec guides list the various codecs that web browsers often support, providing compatibility details along with technical information such as how many audio channels they support, what sort of compression is used, and what bit rates and so forth they're useful at.Thecodecs used by WebRTC guide expands upon this by specifically covering the codecs supported by the major web browsers, so you can choose the codecs that best cover the range of browsers you wish to support.

As for MIME types of audio or video files, they typically specify the container format (file type).The optionalcodecs parameter can be added to the MIME type to further specify which codecs to use and what options were used to encode the media, such as codec profile, level, or other such information.

For more information on common media types, see theCommon MIME types page.

multipart/form-data

Themultipart/form-data type can be used when sending the values of a completedHTML Form from browser to server.

As a multipart document format, it consists of different parts, delimited by a boundary (a string starting with a double dash--).Each part is its own entity with its own HTTP headers,Content-Disposition, andContent-Type for file uploading fields.

http
Content-Type: multipart/form-data; boundary=boundaryString(other headers associated with the multipart document as a whole)--boundaryStringContent-Disposition: form-data; name="myFile"; filename="img.jpg"Content-Type: image/jpeg(data)--boundaryStringContent-Disposition: form-data; name="myField"(data)--boundaryString(more subparts)--boundaryString--

The following<form>:

html
<form  action="http://localhost:8000/"  method="post"  enctype="multipart/form-data">  <label>Name: <input name="myTextField" value="Test" /></label>  <label><input type="checkbox" name="myCheckBox" /> Check</label>  <label>    Upload file: <input type="file" name="myFile" value="test.txt" />  </label>  <button>Send the file</button></form>

will send this message:

http
POST / HTTP/1.1Host: localhost:8000User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateConnection: keep-aliveUpgrade-Insecure-Requests: 1Content-Type: multipart/form-data; boundary=---------------------------8721656041911415653955004498Content-Length: 465-----------------------------8721656041911415653955004498Content-Disposition: form-data; name="myTextField"Test-----------------------------8721656041911415653955004498Content-Disposition: form-data; name="myCheckBox"on-----------------------------8721656041911415653955004498Content-Disposition: form-data; name="myFile"; filename="test.txt"Content-Type: text/plainSimple file.-----------------------------8721656041911415653955004498--

multipart/byteranges

Themultipart/byteranges MIME type is used to send partial responses to the browser.

When the206 Partial Content status code is sent, this MIME type indicates that the document is composed of several parts, one for each of therequested ranges. Like other multipart types, theContent-Type uses aboundary to separate the pieces.Each piece has aContent-Type header with its actual type and aContent-Range of the range it represents.

http
HTTP/1.1 206 Partial ContentAccept-Ranges: bytesContent-Type: multipart/byteranges; boundary=3d6b6a416f9b5Content-Length: 385--3d6b6a416f9b5Content-Type: text/htmlContent-Range: bytes 100-200/1270eta http-equiv="Content-type" content="text/html; charset=utf-8" />    <meta name="viewport" content--3d6b6a416f9b5Content-Type: text/htmlContent-Range: bytes 300-400/1270-color: #f0f0f2;        margin: 0;        padding: 0;        font-family: "Open Sans", "Helvetica--3d6b6a416f9b5--

Importance of setting the correct MIME type

Some server configurations may use the associated MIME type to perform optimizations, such as file concatenation, compression, or caching. Seeh5bp/server-configs-apache for an example of an Apache configuration that compresses files of certain MIME types.

Most web servers send unrecognized resources as theapplication/octet-stream MIME type.For security reasons, most browsers do not allow setting a custom default action (like "Open in Word") for such resources, forcing the user to save it to disk to use it.

Some common incorrect server configurations:

  • RAR-compressed files.In this case, the ideal would be the true type of the original files; this is often impossible as .RAR files can hold several resources of different types.In this case, configure the server to sendapplication/x-rar-compressed.
  • Audio and video.Only resources with the correct MIME Type will be played in<video> or<audio> elements.Be sure to specify the correctmedia type for audio and video.
  • Proprietary file types.A specific type likeapplication/vnd.mspowerpoint lets users open such files automatically in the presentation software of their choice.

MIME sniffing

In the absence of a MIME type, or in certain cases where browsers believe they are incorrect, browsers may performMIME sniffing — guessing the correct MIME type by looking at the bytes of the resource.

Each browser performs MIME sniffing differently and under different circumstances.(For example, Safari will look at the file extension in the URL if the sent MIME type is unsuitable.)There are security concerns as some MIME types represent executable content.Servers can prevent MIME sniffing by sending theX-Content-Type-Options header.

Other methods of conveying document type

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

  • Filename suffixes are sometimes used, especially on Microsoft Windows.Not all operating systems consider these suffixes meaningful (such as Linux and macOS), and there is no guarantee they are correct.
  • Magic numbers. The syntax of different formats allows file-type inference by looking at their byte structure.For example, GIF files start with the47 49 46 38 39 hexadecimal value (GIF89), and PNG files with89 50 4E 47 (.PNG).Not all file types have magic numbers, so this is not 100% reliable either.

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp