大网数据广告活动
Web 标准与前端开发

HTMLInputElement: files property

· 52CE 我爱测

The **`HTMLInputElement.files`** property allows you to access the FileList selected with the `<input type="file">` element.

File and Directory Entries API

The HTMLInputElement.files property allows you to access the FileList selected with the <input type="file"> element.

Value

A FileList object listing the selected files, if any, or null if the HTMLInputElement is not of type="file".

Examples

The example below shows how you can access the HTMLInputElement.files property and log the name, date modified, size, and type of each file selected by the user.

HTML

<input id="files" type="file" multiple />

JavaScript

Note that HTMLInputElement.files still returns an instance of FileList even if no files are selected. Therefore it's safe to iterate through it with Statements/for...of without checking if any files are selected.

const fileInput = document.getElementById("files");

console.log(fileInput.files instanceof FileList); // true even if empty

for (const file of fileInput.files) {
  console.log(file.name); // prints file name
  let fileDate = new Date(file.lastModified);
  console.log(fileDate.toLocaleDateString()); // prints legible date
  console.log(
    file.size < 1000 ? file.size : `${Math.round(file.size / 1000)}KB`,
  );
  console.log(file.type); // prints MIME type
}

Specifications

Browser compatibility

See also

  • DataTransfer.files

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。