非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。
LocalFileSystem
接口在
文件系统 API
gives you access to a sandboxed file system. The methods are implemented by
window
and
worker
对象。
This section includes a few key concepts for the methods.
You request access to a sandboxed file system by calling
window.requestFileSystem().
The argument of a successful callback is the
FileSystem
object, which has two properties: the name and root of the file system.
You can call the method more than once if you want to create two file systems: one that's temporary and one that's persistent. (To learn more about the storage types, see the 基本概念 article.) In most cases, you need to create only one file system, but in a few cases, it might be useful to create a second one. For example, if you were to create a mail app, you might create a temporary storage for caching assets (like images and attachments) to speed up performance, while creating persistent storage for unique data—such as drafts of emails that were composed while offline—that should not be lost before they are backed up into the cloud.
requestFileSystem()
method lets you ask for
PERSISTENT
or
TEMPORARY
storage
. Persistent storage is storage that stays in the browser unless the app or the user removes it, but the user must grant you permission before you can use it. In contrast, temporary storage is automatically granted without any user permission, but it can be expunged by the browser at any time.
要使用
PERSISTENT
storage with the File System API, Chrome exposes a requestQuota API. So to request storage, you need to do something like the following:
var requestedBytes = 1024*1024*10; // 10MB
navigator.webkitPersistentStorage.requestQuota (
requestedBytes, function(grantedBytes) {
window.requestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler);
}, function(e) { console.log('Error', e); }
);
Your user must grant your app permission to store data locally before your app can use persistent storage. Once your user grants it, you don't need to call
requestQuota()
again. Subsequent calls are a noop.
Another API, the Quota Management API, lets you query an origin's current quota usage and allocation using
window.webkitPersistentStorage.queryUsageAndQuota()
. To learn more, see this
StackOverflow Answer
. (An older version of the API is described at
Managing HTML5 Offline Storage
)。
The file system is sandboxed to a single origin. This means that your app cannot read, or write the files of another app's files. Your app cannot access files in an arbitrary folder (such as, My Pictures, My Documents) on the user's hard drive either. For more information about restrictions, see the 基本概念 article.
The following is a code snippet that shows how you can request a file system storage.
//Taking care of the browser-specific prefix window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; // The first parameter defines the type of storage: persistent or temporary // Next, set the size of space needed (in bytes) // initFs is the success callback // And the last one is the error callback // for denial of access and other errors. window.requestFileSystem(window.PERSISTENT, 1024*1024,onInitFs,errorHandler);
void
requestFileSystem
(in unsigned short
type
, in unsigned long long
size
, in FileSystemCallback
successCallback
, in optional ErrorCallback
errorCallback
);
|
void
resolveLocalFileSystemURL
(in DOMString
url
, in EntryCallback
successCallback
, in optional ErrorCallback
errorCallback
);
|
| 常量 | 值 | 描述 |
|---|---|---|
TEMPORARY
|
0
|
Transient storage that can be be removed by the browser at its discretion. |
PERSISTENT
|
1
|
Storage that stays in the browser unless the user or the app expunges it. The user must grant permission before the app can use this type of storage. |
Requests a file system where data should be stored. You access a sandboxed file system by requesting a
LocalFileSystem
object using this global method,
window.requestFileSystem()
.
void requestFileSystem( in unsigned short type, in unsigned long long size, in FileSystemCallback successCallback, in ErrorCallback errorCallback );
TEMPORARY
or
PERSISTENT
.
The storage space—in bytes—that you need for your app.
FileSystem
object with two properties:
DirectoryEntry
object representing the root of the file system.
FileError
对象。
void
This method can raise an FileError 通过以下代码:
| 异常 | 描述 |
|---|---|
SECURITY_ERROR
|
The application does not have permission to access the file system interface. For example, you cannot run from
file://
. For more details, see the
article on basic concepts
.
|
Lets you look up the entry for a file or directory with a local URL.
void resolveLocalFileSystemURL( in DOMString url, in EntryCallback successCallback, in optional ErrorCallback errorCallback );
The URL of a local file in the file system.
The success callback that is called when the browser provides the file or directory for the supplied URL.
The error callback that is called when errors happen or when the request to obtain the entry object is denied.
void
This method can raise an FileError 通过以下代码:
| 异常 | 描述 |
|---|---|
ENCODING_ERR
|
The syntax of the URL was invalid. |
NOT_FOUND_ERR
|
The URL was structurally correct, but refers to a resource that does not exist. |
SECURITY_ERR
|
The application does not have permission to access the file system interface. |
No compatibility data found. Please contribute data for "api.LocalFileSystem" (depth: 1) to the MDN 兼容性数据存储库 .
规范: File API: Directories and System Specification WD
参考: 文件系统 API
介绍: Basic Concepts About the File System API
LocalFileSystem
FileError
FileException
FileHandle
FileRequest
FileSystem
FileSystemDirectoryEntry
FileSystemDirectoryEntrySync
FileSystemDirectoryReader
FileSystemDirectoryReaderSync
FileSystemEntry
FileSystemEntrySync
FileSystemFileEntry
FileSystemFileEntrySync
FileSystemFlags
FileSystemSync
HTMLInputElement
LocalFileSystemSync
LockedFile
元数据
Window.requestFileSystem()
Window.resolveLocalFileSystemURL()
WorkerGlobalScope.requestFileSystemSync()