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

瀏覽器相容性