Return-Path: X-Original-To: apmail-cordova-dev-archive@www.apache.org Delivered-To: apmail-cordova-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5725C18C7F for ; Fri, 1 Apr 2016 22:27:49 +0000 (UTC) Received: (qmail 71038 invoked by uid 500); 1 Apr 2016 22:27:49 -0000 Delivered-To: apmail-cordova-dev-archive@cordova.apache.org Received: (qmail 70999 invoked by uid 500); 1 Apr 2016 22:27:49 -0000 Mailing-List: contact dev-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list dev@cordova.apache.org Received: (qmail 70988 invoked by uid 99); 1 Apr 2016 22:27:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 01 Apr 2016 22:27:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 85A1BDFC6E; Fri, 1 Apr 2016 22:27:48 +0000 (UTC) From: riknoll To: dev@cordova.apache.org Reply-To: dev@cordova.apache.org References: In-Reply-To: Subject: [GitHub] cordova-plugin-file pull request: adding sample section to readme Content-Type: text/plain Message-Id: <20160401222748.85A1BDFC6E@git1-us-west.apache.org> Date: Fri, 1 Apr 2016 22:27:48 +0000 (UTC) Github user riknoll commented on a diff in the pull request: https://github.com/apache/cordova-plugin-file/pull/176#discussion_r58276468 --- Diff: README.md --- @@ -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: ' + fs.name); + 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 -- If you are trying to write a file to the sandboxed storage location, I believe you can just call `fs.root.getFile()` (where `fs` comes from `requestFileSystem`) instead of calling `window.resolveLocalFileSystemURL(cordova.file.dataDirectory, ...)` first. --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org For additional commands, e-mail: dev-help@cordova.apache.org