cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From riknoll <>
Subject [GitHub] cordova-plugin-file pull request: adding sample section to readme
Date Fri, 01 Apr 2016 22:20:00 GMT
Github user riknoll commented on a diff in the pull request:
    --- Diff: ---
    @@ -538,3 +540,263 @@ Android also supports a special filesystem named "documents", which
represents a
     * `root`: The entire device filesystem
     By default, the library and documents directories can be synced to iCloud. You can also
request two additional filesystems, `library-nosync` and `documents-nosync`, which represent
a special non-synced directory within the `/Library` or `/Documents` filesystem.
    +## Sample: Create Files and Directories, Write, Read, and Append files ##
    +The File plugin allows you to do things like store files in a temporary or persistent
storage location for your app (sandboxed storage). The code snippets in this section demonstrate
different tasks including:
    +* Accessing the file system
    +* Using cross-platform Cordova file URLs to store your files (see _Where to Store Files_
for more info)
    +* Creating files and directories
    +* Writing to files
    +* Reading files
    +* Appending files
    +## Create a persistent file
    +Before you can use the File plugin APIs, you must get access to the file system using
`requestFileSystem`. When you do this, you can request either persistent or temporary storage.
Persistent storage will not be removed unless permission is granted by the user.
    +When you get file system access, access is granted for the sandboxed file system only
(the sandbox limits access to the app itself), not for general access to any file system location
on the device. For more information about location-specific URLs, see _Where to Store Files_.
    +Here is a request for persistent storage.
    +>*Note* When targeting devices (instead of a browser), you dont need to use `requestQuota`
before using persistent storage.
    +window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    +    console.log('file system open: ' +;
    +    createPersistentFile("newPersistentFile.txt");
    +}, onErrorLoadFs);
    +Once you have access, you generally want to use the Cordova file URLs, like `cordova.file.dataDirectory`,
where possible (see _Where to Store Files_). This will hide implementation details related
to the file locations. To use a Cordova file URL, call `window.resolveLocalFileSystemURL`.
The success callback receives a DirectoryEntry object as input. You can use this object to
create or get a file (by calling `getFile`).
    +The success callback for `getFile` receives a FileEntry object. You can use this to perform
file write and file read operations.
    +function createPersistentFile(fileName) {
    +    // Return a DirectoryEntry using Cordova file URLs.
    +    window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dirEntry)
    --- End diff --
    You don't actually have to call `window.resolveLocalFileSystemURL` from within `window.requestFileSystem`.
`window.requestFileSystem` is for getting access to the sandboxed application-specific storage
only. `window.resolveLocalFileSystemURL` is for arbitrarily accessing storage (including non-sandboxed
storage locations, like a phone's photo folder) and can be called on its own.

If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at or file a JIRA ticket
with INFRA.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message