We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

返回 File 对象所表示文件的媒体类型(MIME)。


var name = file.type;

字符串,包含媒体类型(MIME),表示文本是什么类型,例如 PNG 图像是 "image/png"。 


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

注: 基于当前的实现,浏览器不会实际读取文件的字节流,来判断它的媒体类型。它基于文件扩展来假设;重命名为 .txt 的 PNG 图像文件为 "text/plain" 而不是 "image/png" 。而且,file.type 仅仅对常见文件类型可靠。例如图像、文档、音频和视频。不常见的文件扩展名会返回空字符串。开发者最好不要依靠这个属性,作为唯一的验证方案。



Specification Status Comment
File API
Working Draft Initial definition.


We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
File.name 13.0 (Yes) 3.6 (1.9.2) 10.0 16.0 (Yes) [1]
Feature Android Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
File.name 未实现 (Yes) 未实现 未实现 未实现 未实现

[1] WebKit bug 32912



此页面的贡献者: wizardforcel
最后编辑者: wizardforcel,