Returns the media type ( MIME ) of the file represented by a File 对象。

句法

var name = file.type;
					

A string, containing the media type(MIME) indicating the type of the file, for example "image/png" for PNG images

范例

<input type="file" multiple onchange="showType(this)">
					
function showType(fileInput) {
  var files = fileInput.files;
  for (var i = 0; i < files.length; i++) {
    var name = files[i].name;
    var type = files[i].type;
    alert("Filename: " + name + " , Type: " + type);
  }
}
					

注意: Based on the current implementation, browsers won't actually read the bytestream of a file to determine its media type. It is assumed based on the file extension; a PNG image file renamed to .txt would give " text/plain " and not " image/png ". Moreover, file.type is generally reliable only for common file types like images, HTML documents, audio and video. Uncommon file extensions would return an empty string. Client configuration (for instance, the Windows Registry) may result in unexpected values even for common types. Developers are advised not to rely on this property as a sole validation scheme.

规范

规范 状态 注释
文件 API
The definition of '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 13 Edge 12 Firefox 3.6 IE 10 Opera 16 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android No Opera Android No Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: