The type property of a Blob object provides the MIME type of the file. It returns an empty string if the type couldn't determined.

Syntax

var mimetype = instanceOfFile.type

Value

A string

Example

var i, fileInput, files, allowedFileTypes;

// fileInput is a HTMLInputElement: <input type="file" multiple id="myfileinput">
fileInput = document.getElementById("myfileinput");

// files is a FileList object (simliar to NodeList)
files = fileInput.files;

// our application only allows *.png, *.jpeg and *.gif images
allowedFileTypes = ["image/png", "image/jpeg", "image/gif"];

for (i = 0; i < files.length; i++) {
  // Test if file.type is an allowed file type.
  if (allowedFileTypes.indexOf(files[i].type) > -1) {
    // file type matched is one of allowed file types. Do something here.
  }
});

Specifications

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

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support512410115.1
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support No No Yes No No No No

See also

Document Tags and Contributors

 Last updated by: fscholz,