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.
var mimetype = instanceOfFile.type
A string
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.
}
});
| Specification | Status | Comment |
|---|---|---|
| File API The definition of 'type' in that specification. | Working Draft | Initial definition. |
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| File.type | 5 | (Yes) | 4.0 (2) | 10.0 | 11.10 | 5.1 |
| Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| File.type | No support | (Yes) | No support | No support | No support | No support |
© 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/Blob/type