此类型对象的返回通过
文件
特性在 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
|
只读值指示列表中的文件数。 |
返回
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 定义。 |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
兼容性未知
FileList