Deze vertaling is niet volledig. Help dit artikel te vertalen vanuit het Engels.
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
De LocalFileSystem
-interface van de File System API geeft je toegang tot een sandboxed bestandssysteem. De methodes zijn geïmplementeerd door window- en worker-objecten.
Over dit document
Dit document is het laatst bijgewerkt op 14 maart 2012 en volgt de W3C Specifications (Working Draft) opgesteld op 19 april 2011.
De specificatie is min of meer verlaten, en niet geslaagd in het krijgen van meer grip.
Basisbegrippen
Deze paragraaf bevat enkele belangrijke concepten voor de methoden.
Creating new storage
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 Basic Concepts 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.
Persistente opslag gebruiken
The 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.
To use PERSISTENT
storage with the File System API, Chrome exposes a new API under window.webkitStorageInfo
. So to request storage, you need to do something like the following:
window.webkitStorageInfo.requestQuota(PERSISTENT, 1024*1024, 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.webkitStorageInfo.queryUsageAndQuota()
. To learn more, see Managing HTML5 Offline Storage.
Werken met een enkele origin
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 Basic Concepts article.
Voorbeeld
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);
Methode-overzicht
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); |
Constanten
Constante | Waarde | Beschrijving |
---|---|---|
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. |
Methoden
requestFileSystem()
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 );
Parameters
- type
-
The storage type of the file system. The values can be either
TEMPORARY
orPERSISTENT
. - size
- The storage space—in bytes—that you need for your app.
- successCallback
-
The success callback that is called when the browser provides a file system. Its argument is the
FileSystem
object with two properties:- name - the unique name assigned by the browser to the file system.
- root - the read-only
DirectoryEntry
object representing the root of the file system.
- opt_errorCallback
-
The error callback that is called when errors happen or when the request to obtain the file system is denied. Its argument is the
FileError
object.
Returns
-
void
Exceptions
This method can raise an FileError with the following code:
Exception | Description |
---|---|
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. |
resolveLocalFileSystemURL()
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 );
Parameters
- url
- The URL of a local file in the file system.
- successCallback
- The success callback that is called when the browser provides the file or directory for the supplied URL.
- errorCallback
- The error callback that is called when errors happen or when the request to obtain the entry object is denied.
Returns
-
void
Exceptions
This method can raise an FileError with the following code:
Exception | Description |
---|---|
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. |
Browsercompatabiliteit
Functie | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basisondersteuning | 13webkit | Not supported | Not supported | Not supported | Not supported |
Functie | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basisondersteuning | Not supported | 0.16 webkit | Not supported | Not supported | Not supported | Not supported |
Zie ook
Specificatie:File API: Directories and System SpecificationWD
Referentie: File System API
Introductie: Basic Concepts About the File System API