FileList

An object of this type is returned by the files property of the HTML <input> element; this lets you access the list of files selected with the <input type="file"> element. It's also used for a list of files dropped into web content when using the drag and drop API; see the DataTransfer object for details on this usage.

注意: 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.

Using the file list

所有 <input> element nodes have a files 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];
					

Method overview

File item (index);

特性

属性 Type 描述
length integer A read-only value indicating the number of files in the list.

方法

item()

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

 File item(
   index
 );
					
参数
index
The zero-based index of the file to retrieve from the list.
返回值

File representing the requested file.

范例

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

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

Here is a complete example.

<!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>
					

规范

规范 状态 Comment
File API
The definition of 'FileList' in that specification.
工作草案
HTML Living Standard
The definition of 'selected files' in that specification.
实时标准

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
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

图例

完整支持
完整支持
兼容性未知
兼容性未知

另请参阅

  1. FileList
  2. Related pages for File API
    1. Blob
    2. File
    3. FileReader
    4. FileReaderSync