W3cubDocs

/DOM

File.type

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 what type of the file is it 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 only reliable for common file types like images, documents audio and video. Uncommon file extensions would return an empty string. Developers are advised not to rely on this property as a sole validation scheme.

Specifications

Specification Status Comment
File API
The definition of 'type' in that specification.
Working Draft Initial definition.

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
File.name 13.0 (Yes) 3.6 (1.9.2) 10.0 16.0 (Yes) [1]
Feature Android Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
File.name No support (Yes) No support No support No support No support

[1] WebKit bug 32912

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/File/type