The type
property of a Blob
object returns the MIME type of the file.
Syntax
var mimetype = blob.type
Value
A DOMString
containing the file's MIME type, or an empty string if the type could not be determined.
Example
This example asks the user to select a number of files, then checks each file to make sure it's one of a given set of image file types.
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 GIF, PNG, and JPEG 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 'Blob.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.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
type | Chrome Full support 5 | Edge Full support 12 | Firefox Full support 4 | IE Full support 10 | Opera Full support 11 | Safari Full support 5.1 | WebView Android No support No | Chrome Android Full support 18 | Firefox Android No support No | Opera Android No support No | Safari iOS No support No | Samsung Internet Android Full support 1.0 |
Legend
- Full support
- Full support
- No support
- No support