此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

File:name 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

备注: 此特性在 Web Worker 中可用。

File 接口的 name 只读属性返回由 File 对象表示的文件的名称。由于安全原因,该属性并不包含文件路径。

一个字符串,包含不带路径的文件名,例如“My Resume.rtf”。

示例

HTML

html
<input type="file" id="file-picker" multiple /> <div> <p>选定文件列表:</p> <ul id="output"></ul> </div> 

JavaScript

js
const output = document.getElementById("output"); const filePicker = document.getElementById("file-picker"); filePicker.addEventListener("change", (event) => { const files = event.target.files; output.textContent = ""; for (const file of files) { const li = document.createElement("li"); li.textContent = file.name; output.appendChild(li); } }); 

结果

规范

Specification
File API
# dfn-name

浏览器兼容性

参见