The type
property of a Blob
object returns the MIME type of the file.
On this page
Blob: type property
Value
A string containing the file's MIME type, or an empty string if the type could not be determined.
Examples
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.
HTML
html
<input type="file" id="input" multiple />
<output id="output">Choose image files…</output>
JavaScript
js
// Our application only allows GIF, PNG, and JPEG images
const allowedFileTypes = ["image/png", "image/jpeg", "image/gif"];
const input = document.getElementById("input");
const output = document.getElementById("output");
input.addEventListener("change", (event) => {
const files = event.target.files;
if (files.length === 0) {
output.innerText = "Choose image files…";
return;
}
const allAllowed = Array.from(files).every((file) =>
allowedFileTypes.includes(file.type),
);
output.innerText = allAllowed
? "All files clear!"
: "Please choose image files only.";
});
Result
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 | 6 | 4.4 | 18 | 4 | 11 | 6 | 1.0 |
See also
© 2005–2023 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