FileList:length 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

注意:此功能在 Web Workers 中可用。

FileList 接口的length只读属性返回 FileList 中文件的数量。

一个数字,表示列表中文件的数量。

示例

打印所选文件的数量

在此示例中,我们使用 length 来查找 FileList 中项目的数量。

HTML

html
<input type="file" multiple />
<div class="output"></div>

JavaScript

js
const fileInput = document.querySelector("input[type=file]");
const output = document.querySelector(".output");

fileInput.addEventListener("change", () => {
  const fileList = fileInput.files;
  output.textContent = `You've selected: ${fileList.length} file(s)`;
});

结果

规范

规范
File API
# dfn-length

浏览器兼容性