非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。
文件和目录条目 API simulates a local file system that web apps can navigate around. You can develop apps that can read, write, and create files and directories in a sandboxed, virtual file system.
The File and Directory Entries API interacts with other related APIs. It was built on the File Writer API, which, in turn, was built on File API. Each of the APIs adds different functionality. These APIs are a giant evolutionary leap for web apps, which can now cache and process large amounts of data.
This introduction discusses essential concepts and terminology in the File and Directory Entries API. It gives you the big picture and orients you to key concepts . It also describes restrictions that raise security errors if you ignore them. To learn more about terminology used in this API, see the Definitions 章节。
For the reference documentation on the File and Directory Entries API, see the reference landing page and its subpages.
The specification is still being defined and is subject to change.
The File and Directory Entries API includes both asynchronous and synchronous versions of the interfaces. The asynchronous API can be used in cases where you don't want an outstanding operation to block the UI. The synchronous API, on the other hand, allows for simpler programming model, but it must be used with WebWorkers .
The File and Directory Entries API is an important API for the following reasons:
For examples of features you can create with this app, see the Sample use cases 章节。
The File and Directory Entries API is an alternative to other storage APIs like IndexedDB , WebSQL (which has been deprecated since November18, 2010), and AppCache. The API is a better choice for apps that deal with blobs for the following reasons:
The following are just a few examples of how you can use the File and Directory Entries API:
Before you start using the File and Directory Entries API, you need to understand a few concepts:
The API doesn't give you access to the local file system, nor is the sandbox really a section of the file system. Instead, it is a virtualized file system that looks like a full-fledged file system to the web app. It does not necessarily have a relationship to the local file system outside the browser.
What this means is that a web app and a desktop app cannot share the same file at the same time. The API does not let your web app reach outside the browser to files that desktop apps can also work on. You can, however, export a file from a web app to a desktop app. For example, you can use the File API, create a blob, redirect an iframe to the blob, and invoke the download manager.
An application can request temporary or persistent storage. Temporary storage is easier to get, because the browser just gives it to you, but it is limited and can be deleted by the browser when it runs out of space. Persistent storage, on the other hand, might offer you larger space that can only be deleted by the user, but it requires the user to grant you permission.
Use temporary storage for caching and persistent storage for data that you want your app to keep—such as user-generated or unique data.
To prevent a web app from using up the entire disk, browsers might impose a quota for each app and allocate storage among web apps.
How storage space is granted or allocated and how you can manage storage are idiosyncratic to the browser, so you need to check the respective documentation of the browser. Google Chrome, for example, allows temporary storage beyond the 5 MB required in the specifications and supports the Quota Management API. To learn more about the Chrome-specific implementation, see Managing HTML5 Offline Storage .
The File and Directory Entries API comes with asynchronous and synchronous versions. Both versions of the API offer the same capabilities and features. In fact, they are almost alike, except for a few differences.
requestFileSystem()
and
resolveLocalFileSystemURL()
. These methods are members of both the window object and the worker global scope. The synchronous API, on the other hand, uses the following methods:
requestFileSystemSync()
and
resolveLocalFileSystemSyncURL()
. These synchronous methods are members of the worker's global scope only, not the window object.
The synchronous API can be simpler for some tasks. Its direct, in-order programming model can make code easier to read. The drawback of synchronous API has to do with its interactions with Web Workers, which has some limitations.
When using the asynchronous API, always use the error callbacks. Although the error callbacks for the methods are optional parameters, they are not optional for your sanity. You want to know why your calls failed. At minimum, handle the errors to provide error messages, so you'll have an idea of what's going on.
The File and Directory Entries API is designed to be used with other APIs and elements on the web platform. For example, you are likely to use one of the following:
send()
method for file and blob objects)
input
element (to programmatically obtain a list of files from the element)
For security reasons, browsers impose restrictions on file access. If you ignore them, you will get security errors.
An origin is the domain, application layer protocol, and port of a URL of the document where the script is being executed. Each origin has its own associated set of file systems.
The security boundary imposed on file system prevents applications from accessing data with a different origin. This protects private data by preventing access and deletion. For example, while an app or a page in http://www.example.com/app/ can access files from http://www.example.com/dir/ , because they have the same origin, it cannot retrieve files from http://www.example.com:8080/dir/ (different port) or https://www.example.com/dir/ (different protocol).
To prevent malicious apps from running hostile executables, you cannot create executable files within the sandbox of the File and Directory Entries API.
Because the file system is sandboxed, a web app cannot access another app's files. You also cannot read or write files to an arbitrary folder (for example, My Pictures and My Documents) on the user's hard drive.
You cannot run your app locally from
file://
. If you do so, the browser throws errors or your app fails silently. This restriction also applies to many of the file APIs, including BlobBuilder and FileReader.
For testing purposes, you can bypass the restriction on Chrome by starting the browser with the
--allow-file-access-from-files
flag. Use this flag only for this purpose.
This section defines and explains terms used in the File and Directory Entries API.
Stands for binary large object. A blob is a set of binary data that is stored as a single object. It is a general-purpose way to reference binary data in web applications. A blob can be an image or an audio file.
Blob—with a capital B—is a data structure that is immutable, which means that binary data referenced by a Blob cannot be modified directly. This makes Blobs act predictably when they are passed to asynchronous APIs.
Persistent storage is storage that stays in the browser unless the user expunges it or the app deletes it.
Transient storage is available to any web app. It is automatic and does not need to be requested, but the browser can delete the storage without warning.
| 规范 | 状态 | 注释 |
|---|---|---|
| 文件和目录条目 API | 草案 | 提议 API 草案 |
此 API 没有正式的 W3C 或 WHATWG (Web 超文本应用程序技术工作组) 规范。
FileSystem
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
FileSystem
|
Chrome
7
Alternate Name
|
Edge
≤18
Prefixed
|
Firefox 50 | IE 不支持 No |
Opera
15
Prefixed
|
Safari 11.1 |
WebView Android
≤37
Alternate Name
|
Chrome Android
18
Alternate Name
|
Firefox Android 50 |
Opera Android
14
Prefixed
|
Safari iOS 11.3 |
Samsung Internet Android
1.0
Prefixed
|
完整支持
不支持
见实现注意事项。
使用非标名称。
要求使用供应商前缀或不同名称。
FileSystemSync
property
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
FileSystemSync
非标
|
Chrome
13
Prefixed
|
Edge
≤79
Prefixed
|
Firefox 不支持 No | IE 不支持 No |
Opera
15
Prefixed
|
Safari
6
Prefixed
|
WebView Android
≤37
Prefixed
|
Chrome Android
18
Prefixed
|
Firefox Android 不支持 No |
Opera Android
14
Prefixed
|
Safari iOS
6
Prefixed
|
Samsung Internet Android
1.0
Prefixed
|
完整支持
不支持
非标。预期跨浏览器支持较差。
要求使用供应商前缀或不同名称。
FileError
FileException
FileHandle
FileRequest
FileSystemDirectoryEntry
FileSystemDirectoryEntrySync
FileSystemDirectoryReader
FileSystemDirectoryReaderSync
FileSystemEntry
FileSystemEntrySync
FileSystemFileEntry
FileSystemFileEntrySync
FileSystemFlags
FileSystem
FileSystemSync
HTMLInputElement
LocalFileSystem
LocalFileSystemSync
LockedFile
元数据