Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file/pull/176#discussion_r58442897
  
    --- 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.
    +
    +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);
    +    fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: 
false }, function (fileEntry) {
    +
    +        console.log("fileEntry is file?" + fileEntry.isFile.toString());
    +        // fileEntry.name == 'someFile.txt'
    +        // fileEntry.fullPath == '/someFile.txt'
    +        writeFile(fileEntry, null);
    +
    +    }, onErrorCreateFile);
    +
    +}, onErrorLoadFs);
    +```
    +
    +The success callback receives FileSystem object (fs). Use `fs.root` to 
return a DirectoryEntry object, which you can use to create or get a file (by 
calling `getFile`). In this example, `fs.root` is a DirectoryEntry object that 
represents the persistent storage in the sandboxed file system.
    +
    +The success callback for `getFile` receives a FileEntry object. You can 
use this to perform file write and file read operations.
    +
    +## Create a temporary file
    +
    +Here is an example of a request for temporary storage. Temporary storage 
may be deleted by the operating system if the device runs low on memory.
    +
    +```
    +window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
    +
    +    console.log('file system open: ' + fs.name);
    +    createFile(fs.root, "newTempFile.txt");
    +
    +}, onErrorLoadFs);
    +```
    +When you are using temporary storage, you can create or get the file by 
calling `getFile`. As in the persistent storage example, this will give you a 
FileEntry object that you can use for read or write operations.
    +
    +```
    +// Creates a new file or returns the file if it already exists.
    +dirEntry.getFile(fileName, {create: true, exclusive: false}, 
function(fileEntry) {
    +
    +    writeFile(fileEntry);
    +
    +}, onErrorCreateFile);
    +```
    +
    +## Write to a file
    +
    +Once you have a FileEntry object, you can write to the file by calling 
`createWriter`, which returns a FileWriter object in the success callback. Call 
the `write` method of FileWriter to write to the file.
    +
    +```
    +function writeFile(fileEntry, dataObj) {
    +    // Create a FileWriter object for our FileEntry (log.txt).
    +    fileEntry.createWriter(function (fileWriter) {
    +
    +        fileWriter.onwriteend = function (e) {
    +            console.log("Successful file read...");
    +            readFile(fileEntry);
    +        };
    +
    +        fileWriter.onerror = function (e) {
    +            console.log("Failed file read: " + e.toString());
    +        };
    +
    +        // If data object is not passed in,
    +        // create a new Blob instead.
    +        if (!dataObj) {
    +            dataObj = new Blob(['some file data'], { type: 'text/plain' });
    +        }
    +
    +        fileWriter.write(dataObj);
    +    });
    +}
    +```
    +
    +## Read a file
    +
    +You also need a FileEntry object to read an existing file. Use the file 
property of FileEntry to get the file reference, and then create a new 
FileReader object. You can use methods like `readAsText` to start the read 
operation. When the read operation is complete, `this.result` stores the result 
of the read operation.
    +
    +```
    +function readFile(fileEntry) {
    +    fileEntry.file(function (file) {
    +        var reader = new FileReader();
    +
    +        reader.onloadend = function (e) {
    +            console.log("Successful file read: " + this.result);
    +            displayFileData(fileEntry.fullPath + ": " + this.result);
    +            if (this.result.toString().substring(0, 4) == "blob") {
    +                displayImageData(this.result);
    +            }
    +        };
    +
    +        reader.readAsText(file);
    +
    +    }, onErrorReadFile);
    +}
    +```
    +
    +## Store an existing file
    +
    +We already showed how to write to a file that you just created in the 
sandboxed file system. What if you need to get access to an existing file and 
convert that to something you can store on your device? In this example, you 
obtain a file using an xhr request, and then save it to the cache in the 
sandboxed file system.
    +
    +Before you get the file, get a FileSystem reference using 
`requestFileSystem`. By passing window.TEMPORARY in the method call (same as 
before), the returned FileSystem object (fs) represents the cache in the 
sandboxed file system. Call `fs.root` to get the DirectoryEntry object.
    +
    +```
    +window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
    +
    +    console.log('file system open: ' + fs.name);
    +    getSampleFile(fs.root);
    +
    +}, onErrorLoadFs);
    +```
    +
    +For completeness, here is the xhr request to get a Blob image. There is 
nothing Cordova-specific in this code, except that you forward the 
DirectoryEntry reference that you already obtained as an argument to the 
saveFile function. You will save the image as a DOM string URL and display it 
later after reading the file (to validate the operation).
    +
    +```
    +function getSampleFile(dirEntry) {
    +
    +    var xhr = new XMLHttpRequest();
    +    xhr.open('GET', 
'http://cordova.apache.org/static/img/cordova_bot.png', true);
    +    xhr.responseType = 'blob';
    +
    +    xhr.onload = function (e) {
    +        if (this.status == 200) {
    +
    +            var blob = new Blob([this.response], { type: 'image/png' });
    +            var img = new Image();
    +            // Note: Use window.URL.revokeObjectURL when finished with 
image.
    +            img.src = window.URL.createObjectURL(blob);
    +
    +            saveFile(dirEntry, img.src, "downloadedImage.png");
    +        }
    +    };
    +    xhr.send();
    +}
    +```
    +>*Note* For Cordova 5 security, the preceding code requires that you add 
the domain name, http://cordova.apache.org, to the Content-Security-Policy 
<meta> element in index.html.
    +
    +After getting the file, copy the contents to a new file. The current 
DirectoryEntry object is already associated the app cache.
    +
    +```
    +function saveFile(dirEntry, srcImage, fileName) {
    +
    +    dirEntry.getFile(fileName, { create: true, exclusive: false }, 
function (fileEntry) {
    +
    +        writeFile(fileEntry, srcImage);
    +
    +    }, onErrorCreateFile);
    +}
    +```
    +
    +In writeFile, you pass in the DOM string URL as the dataObj and you will 
save that in the new file.
    +
    +```
    +function writeFile(fileEntry, dataObj) {
    +    // Create a FileWriter object for our FileEntry (log.txt).
    +    fileEntry.createWriter(function (fileWriter) {
    +
    +        fileWriter.onwriteend = function (e) {
    +            console.log("Successful file read...");
    +            readFile(fileEntry);
    +        };
    +
    +        fileWriter.onerror = function (e) {
    +            console.log("Failed file read: " + e.toString());
    +        };
    +
    +        // If data object is not passed in,
    +        // create a new Blob instead.
    +        if (!dataObj) {
    +            dataObj = new Blob(['some file data'], { type: 'text/plain' });
    +        }
    +
    +        fileWriter.write(dataObj);
    +    });
    +}
    +```
    +
    +After writing to the file, read it and display it. These operations re-use 
the code that we showed you already in previous tasks, so theres nothing new 
there (see the previous sections). After reading the data, you can display the 
image using code like this.
    +
    +```
    +function displayImageData(fileData) {
    +
    +    // Displays image if result is a valid DOM string for an image.
    +    var elem = document.getElementById('imageFile');
    +    elem.src = fileData;
    +}
    +```
    +
    +## Create Directories
    +
    +In the code here, you create directories in the root of the app storage 
location. You could use this code with any writable storage location (that is, 
any DirectoryEntry). Here, you write to the application root directory 
(fs.root), which is passed into this function.
    +
    +This code creates the /NewDirInRoot/images folder in the root of the app 
storage location. For platform-specific values, look at _File System Layouts_.
    +
    +```
    +function createDirectory(rootDirEntry) {
    +    rootDirEntry.getDirectory('NewDirInRoot', { create: true }, function 
(dirEntry) {
    +        dirEntry.getDirectory('images', { create: true }, function 
(subDirEntry) {
    +
    +            createFile(subDirEntry, "fileInNewSubDir.txt");
    +
    +        }, onErrorGetDir);
    +    }, onErrorGetDir);
    +}
    +```
    +
    +When creating subfolders, you need to create each folder separately as 
shown in the preceding code.
    +
    +## Append a file
    +
    +Of course, you will often want to append existing files instead of 
creating new ones. Here is an example of that. In this version of the writeFile 
function, you check whether an append operation is requested.
    +
    +Once you have a FileWriter object, call the `seek` method, and pass in the 
index value for the position where you want to write. In this example, you also 
test whether the file exists. After calling seek, then call the write method of 
FileWriter.
    +
    +```
    +function writeFile(fileEntry, dataObj, isAppend) {
    +    // Create a FileWriter object for our FileEntry (log.txt).
    +    fileEntry.createWriter(function (fileWriter) {
    +
    +        fileWriter.onwriteend = function (e) {
    +            console.log("Successful file read...");
    +            readFile(fileEntry);
    +        };
    +
    +        fileWriter.onerror = function (e) {
    +            console.log("Failed file read: " + e.toString());
    --- End diff --
    
    Failed file WRITE, not read. Also 5 lines above.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org

Reply via email to