Returns the media type (MIME) of the file represented by a File
object.
Syntax
var name = file.type;
Value
A string, containing the media type(MIME) indicating the type of the file, for example "image/png" for PNG images
Example
<input type="file" multiple onchange="showType(this)">
function showType(fileInput) {
var files = fileInput.files;
for (var i = 0; i < files.length; i++) {
var name = files[i].name;
var type = files[i].type;
alert("Filename: " + name + " , Type: " + type);
}
}
Note: Based on the current implementation, browsers won't actually read the bytestream of a file to determine its media type. It is assumed based on the file extension; a PNG image file renamed to .txt would give "text/plain" and not "image/png". Moreover, file.type
is generally reliable only for common file types like images, HTML documents, audio and video. Uncommon file extensions would return an empty string. Client configuration (for instance, the Windows Registry) may result in unexpected values even for common types. Developers are advised not to rely on this property as a sole validation scheme.
Specifications
Specification | Status | Comment |
---|---|---|
File APIThe definition of 'type' in that specification. | Working Draft | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
type
|
Chrome
Full support 13 |
Edge
Full support 12 |
Firefox
Full support 3.6 |
IE
Full support 10 |
Opera
Full support 16 |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
No support No |
Opera Android
No support No |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
See also
File.type by Mozilla Contributors is licensed under CC-BY-SA 2.5.