Blob.type
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 (similar 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 |
---|
File API # dfn-type |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
type |
5 |
12 |
4 |
10 |
11 |
5.1 |
≤37 |
18 |
No |
No |
No |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Blob/type