Blob.type

Blob 对象的 type 属性给出文件的 MIME 类型。如果类型无法确定,则返回空字符串。

语法

var mimetype = instanceOfFile.type

字符串

例子

// fileInput是一个 HTMLInputElement,HTML Input 元素: <input type="file" multiple id="myfileinput">
var fileInput = document.getElementById("myfileinput");

// files 是一个 FileList 对象(与 NodeList 相似,是多个 File 对象的集合)
var files = fileInput.files;

// 仅允许 *.png, *.jpeg *.gif 类型的图片文件
var allowedFileTypes = ["image/png", "image/jpeg", "image/gif"];

for (var i = 0; i < files.length; i++) {
    // 检查文件的文件类型是否属于 allowFileTypes 中的一种
    if (allowedFileTypes.indexOf(files[i].type) > -1) {
        // 对符合条件的文件进行处理
    }
}

规范

规范 状态 备注
File API
type
Working Draft Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
typeChrome Full support 5Edge Full support 12Firefox Full support 4IE Full support 10Opera Full support 11Safari Full support 5.1WebView Android No support NoChrome Android Full support 18Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support

参见