type property of a Blob object returns the MIME 类型 of the file.

句法

var mimetype = blob.type
					

A DOMString containing the file's MIME type, or an empty string if the type could not be determined.

范例

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.
  }
});
					

规范

规范 状态 注释
文件 API
The definition of 'Blob.type' in that specification.
工作草案 初始定义。

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
type Chrome 5 Edge 12 Firefox 4 IE 10 Opera 11 Safari 5.1 WebView Android 不支持 No Chrome Android 18 Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 1.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: