# 文件系统访问 API

文件系统访问 API（File System Access API）是一个 Web 平台 API，由 Web 平台孵化器社区组（WICG）开发，它让 Web 应用能够打开用户选定的文件和文件夹、读取它们，并把改动直接写回磁盘上的同一批文件，而不是先上传副本再下载新版本。<sup>[1](https://progressivewebapps.com/capabilities/file-system/)</sup> 它建立在用于读取的 File API 之上，并新增了修改文件和处理目录的接口。<sup>[2](https://wicg.github.io/file-system-access/)</sup> 它与 File and Directory Entries API 中名称相似的 `FileSystem` 接口不同，后者代表的是浏览器内部一个沙箱化的“虚拟驱动器”，而非用户真实的文件系统。<sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup><sup> • </sup><sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem)</sup>

| 关键事实 | 详情 |
|---|---|
| 入口点 | 三个选择器：`showOpenFilePicker()`、`showSaveFilePicker()` 和 `showDirectoryPicker()`，合称本地文件系统句柄工厂<sup>[2](https://wicg.github.io/file-system-access/)</sup> |
| 核心对象 | `FileSystemHandle`，其子类 `FileSystemFileHandle` 和 `FileSystemDirectoryHandle` 分别代表选中的文件或目录<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)</sup> |
| 要求 | 仅在安全上下文（HTTPS）中可用，且只能由用户手势触发<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)</sup><sup> • </sup><sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup> |
| 权限模型 | 选中文件即授予读取权限；写入会触发单独的权限提示，每个句柄各自带有自己的权限状态<sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup> |
| 浏览器支持 | Chrome 86、Edge 86、Opera 72、Samsung Internet 14.0，以及 Safari 15.2（部分支持）；Firefox 不支持<sup>[6](https://mdn2.netlify.app/en-us/docs/web/api/file_system_access_api/)</sup> |
| 标准状态 | 由 WICG 发布；不是 W3C 标准，也不在 W3C 标准轨道上<sup>[2](https://wicg.github.io/file-system-access/)</sup> |
| 知名使用者 | 根据 2023 年 USENIX Security 的一项研究，包括 vscode.dev（微软 Visual Studio Code 的 Web 版）和 Snapchat<sup>[7](https://www.usenix.org/system/files/usenixsecurity23-oz.pdf)</sup> |

## 工作原理

与文件和目录的大多数交互都通过句柄进行。调用 `window.showOpenFilePicker()` 会显示一个文件选择器对话框；当用户选中一个文件时，该 API 返回一个 `FileSystemFileHandle` 对象数组，其中包含与该文件交互所需的属性和方法。调用 `handle.getFile()` 会返回一个由 blob 支持的 `File` 对象，其内容可以用 `stream()`、`text()`、`arrayBuffer()` 读取，若要随机访问则可用 `slice()`。如果磁盘上的底层文件发生变化，该 `File` 对象就会变得不可读，应用必须再次调用 `getFile()`。<sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup>

**保存与写入。** `showSaveFilePicker()` 以保存模式显示选择器，让用户选择用于保存的新文件。写入通过事务性的 `FileSystemWritableFileStream` 进行。<sup>[1](https://progressivewebapps.com/capabilities/file-system/)</sup> 当应用想要把改动保存到它此前拥有读取权限的文件时，浏览器会显示一个请求写入权限的提示；该请求只能由用户手势触发，例如点击“保存”按钮。<sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup> 一个常见的错误是在调用 `showSaveFilePicker()` 之前进行耗时的数据处理，这会使用户手势的有效期届满，导致不再满足调用要求，并抛出 `SecurityError`；推荐的做法是先获取句柄，再处理数据。<sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup>

**其他获取句柄的方式。** 除了选择器之外，句柄还可以来自 HTML 拖放 API 中的 `DataTransferItem.getAsFileSystemHandle()`，以及 File Handling API——后者让已安装的[渐进式 Web 应用](https://www.edgepedia.org/zh/progressive-web-app)注册为特定文件类型的“打开方式”目标，并以 `FileSystemFileHandle` 对象的形式接收被打开的文件。<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)</sup><sup> • </sup><sup>[8](https://progressivewebapps.com/capabilities/file-handling/)</sup> 句柄还可以序列化进 [IndexedDB](https://www.edgepedia.org/zh/indexeddb) 数据库，或通过 `postMessage()` 传递，这样应用就能跨会话保留访问权限。<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)</sup>

## 安全与权限模型

除非用户明确许可，否则不允许访问文件和目录数据。始终由用户挑选网站可以接触的文件或目录，而用户代理将“选中某个文件”视为有意授予对该文件的读取权限。<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)</sup><sup> • </sup><sup>[2](https://wicg.github.io/file-system-access/)</sup> 在第三方上下文中，例如来源与顶层框架不匹配的 iframe，网站无法通过选择器或对现有句柄请求更多权限来访问它原本无权访问的数据。<sup>[2](https://wicg.github.io/file-system-access/)</sup>

[Chromium](https://www.edgepedia.org/zh/chromium-web-browser) 会阻止访问敏感位置，包括系统文件夹、浏览器配置文件、`~/.ssh`、`~/Library` 和 `/etc`，并在保存危险文件类型之前发出警告。<sup>[1](https://progressivewebapps.com/capabilities/file-system/)</sup> WICG 的说明文档还指出了该设计必须遏制的核心风险：网站读取私人数据带来的隐私风险，以及网站修改可执行文件、安装病毒，或加密用户数据并索要赎金带来的安全风险。<sup>[9](https://github.com/WICG/file-system-access/blob/main/EXPLAINER.md)</sup>

**已证实的滥用。** 2023 年 USENIX Security 研讨会上的研究人员构建了 RØB——一个可运行的勒索软件变种，以 Web 应用形式投递，在用户被骗授予读写权限后从浏览器中加密用户的文件。他们指出，该权限模型的有效性有限，因为攻击者可以通过社会工程隐藏其意图；而封锁关键目录也不够，因为子目录和未被封锁的目录（例如云盘目录）仍然可以被加密。<sup>[7](https://www.usenix.org/system/files/usenixsecurity23-oz.pdf)</sup> 另一项 2025 年的研究表明，可以通过该 API 直接从 Web 浏览器对浏览器用户配置文件发起强力攻击。<sup>[10](https://arxiv.org/abs/2504.17692)</sup>

## 浏览器立场与支持情况

该 API 在 Windows、macOS、ChromeOS、Linux 和 Android 上的大多数 Chromium 浏览器中得到支持，[Brave](https://www.edgepedia.org/zh/brave-web-browser) 是显著的例外——在 Brave 中它只能通过标志启用。<sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup> 版本支持为 Chrome 86、Edge 86、Opera 72、Samsung Internet 14.0 和 [Safari](https://www.edgepedia.org/zh/safari-web-browser) 15.2，Firefox、Internet Explorer 和 Android WebView 均不支持。<sup>[6](https://mdn2.netlify.app/en-us/docs/web/api/file_system_access_api/)</sup>

**其他引擎的部分采纳。** Mozilla 将该 API 标记为有害，称其认为“无法获得有意义的最终用户同意”，而 Brave 认为它“非标准且带来隐私风险”。<sup>[7](https://www.usenix.org/system/files/usenixsecurity23-oz.pdf)</sup> Mozilla 的标准立场补充说，它对作为替代存储端点的文件与目录读写 API 抱有热情，但反对与之捆绑的跨站本地文件系统访问。<sup>[11](https://github.com/mozilla/standards-positions/blob/main/activities.yml)</sup> WebKit 对其中本地文件系统部分的立场是反对；其问题追踪器记录的观点是，无法在保障最终用户利益的前提下授予对最终用户本地文件系统的写入权限。<sup>[12](https://github.com/WebKit/standards-positions/issues/28)</sup> 尽管如此，Safari 仍实现了文件系统标准的源私有文件系统，在 macOS 12.2 及以上和 iOS 15.2 及以上的 Safari 中可用。<sup>[13](https://webkit.org/blog/12257/the-file-system-access-api-with-origin-private-file-system/)</sup>

## 源私有文件系统

源私有文件系统（OPFS）是一个存储端点，作为更广泛的文件系统 API 的一部分提供，它仅供该页面所属的源访问，不像常规文件系统那样对用户可见。它提供一种为性能高度优化的特殊文件，支持对其内容进行原地写入。<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)</sup> 从概念上讲，每个源拥有一个独立目录，页面只能访问自己源目录中的文件；一个 OPFS 条目不一定对应用户本地文件系统中的一个条目，它可能是存储在数据库中的对象，因此在其中创建的文件可能无法从浏览器外部取回。<sup>[13](https://webkit.org/blog/12257/the-file-system-access-api-with-origin-private-file-system/)</sup> 在 Chromium 102 及更高版本中，`createSyncAccessHandle()` 提供同步读写操作；它暴露在 `FileSystemFileHandle` 上，但仅限于 Web Worker 中使用。<sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup>

## 历史与实用指南

该 API 在 Chrome 78 至 80 中以“原生文件系统 API”（Native File System API）之名进行了源试用，入口点为 `window.chooseFileSystemEntries()`；该方法在 API 正式发布前被移除，检测它的旧教程会在拥有当前 API 的浏览器中报告“不支持”。<sup>[1](https://progressivewebapps.com/capabilities/file-system/)</sup> Chrome 122 增加了持久化的文件系统访问权限，使已安装的应用能够跨会话保留文件授权。<sup>[8](https://progressivewebapps.com/capabilities/file-handling/)</sup>

由于支持情况参差不齐，标准的建议是基于 `<input type="file">`、`<a download>` 或诸如 browser-fs-access 之类的兼容库来构建，并把文件系统访问 API 视为渐进增强。<sup>[1](https://progressivewebapps.com/capabilities/file-system/)</sup> 特性检测应检查选择器方法本身，例如 `'showOpenFilePicker' in self`。<sup>[3](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)</sup>

## References

1. [File System Access API: Pickers, Handles and Writes, Progressive Web Apps](https://progressivewebapps.com/capabilities/file-system/)
2. [File System Access, WICG specification](https://wicg.github.io/file-system-access/)
3. [The File System Access API: simplifying access to local files, Chrome for Developers](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access?authuser=1)
4. [FileSystem, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem)
5. [File System API, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)
6. [File System Access API browser compatibility (MDN mirror data)](https://mdn2.netlify.app/en-us/docs/web/api/file_system_access_api/)
7. [RØB: Ransomware over Modern Web Browsers, USENIX Security 2023](https://www.usenix.org/system/files/usenixsecurity23-oz.pdf)
8. [File Handling API: Make Your PWA an Open With Target, Progressive Web Apps](https://progressivewebapps.com/capabilities/file-handling/)
9. [File System Access Explainer, WICG GitHub](https://github.com/WICG/file-system-access/blob/main/EXPLAINER.md)
10. [User Profiles: The Achilles' Heel of Web Browsers, arXiv 2025](https://arxiv.org/abs/2504.17692)
11. [Mozilla standards positions, activities.yml](https://github.com/mozilla/standards-positions/blob/main/activities.yml)
12. [File System Access API (Local Filesystem), WebKit standards-positions issue 28](https://github.com/WebKit/standards-positions/issues/28)
13. [The File System API with Origin Private File System, WebKit blog](https://webkit.org/blog/12257/the-file-system-access-api-with-origin-private-file-system/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies › Web platform APIs

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/file-system-access-api

English original: https://www.edgepedia.org/file-system-access-api
