此类型对象的返回通过 文件 特性在 HTML <input> 元素;这允许您访问选择文件列表采用 <input type="file"> 元素。它还被用于把文件列表拖入 Web 内容当使用拖放 API 时。见 DataTransfer 对象了解此用法的细节。

注意: Prior to Gecko 1.9.2 , the input element only supported a single file being selected at a time, meaning that the FileList would contain only one file. Starting with Gecko 1.9.2 , if the input element's multiple attribute is true, the FileList may contain multiple files.

使用文件列表

所有 <input> element nodes have a 文件 attribute of type FileList on them which allows access to the items in this list. For example, if the HTML includes the following file input:

<input id="fileItem" type="file">
					

The following line of code fetches the first file in the node's file list as a File 对象:

var file = document.getElementById('fileItem').files[0];
					

方法概述

File item (index);

特性

属性 类型 描述
length integer 只读值指示列表中的文件数。

方法

item()

返回 File object representing the file at the specified index in the file list.

 File item(
   index
 );
					
参数
index

从列表检索从 0 开始的文件索引。

返回值

File 表示请求文件。

范例

This example iterates over all the files selected by the user using an input 元素:

// fileInput is an HTML input element: <input type="file" id="myfileinput" multiple>
var fileInput = document.getElementById("myfileinput");
// files is a FileList object (similar to NodeList)
var files = fileInput.files;
var file;
// loop through files
for (var i = 0; i < files.length; i++) {
    // get item
    file = files.item(i);
    //or
    file = files[i];
    alert(file.name);
}
					

这里是完整范例。

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<!--multiple is set to allow multiple files to be selected-->
<input id="myfiles" multiple type="file">
</body>
<script>
var pullfiles=function(){
    // love the query selector
    var fileInput = document.querySelector("#myfiles");
    var files = fileInput.files;
    // cache files.length
    var fl = files.length;
    var i = 0;
    while ( i < fl) {
        // localize file var in the loop
        var file = files[i];
        alert(file.name);
        i++;
    }
}
// set the input element onchange to call pullfiles
document.querySelector("#myfiles").onchange=pullfiles;
//a.t
</script>
</html>
					

规范

规范 状态 注释
文件 API
在该规范中的 FileList 定义。
工作草案
HTML 实时标准
在该规范中的 selected files 定义。
实时标准

浏览器兼容性

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
FileList Chrome 1 Edge 12 Firefox 3 IE 10 Opera 11.1 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 11.1 Safari iOS 3.2 Samsung Internet Android 1.0
item Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
length Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: