Phonegap filesystem

38 views
Skip to first unread message

Doc Jump

unread,
Jul 24, 2016, 7:58:25 PM7/24/16
to phonegap
Can anyone suggest an online source to obtain the CORRECT file system writing documentation.

I have tried all online examples and none work. In particular, I tried this github cordova-plugin-file documentation::

function requestFS()
{
confirm("requestfs begin");
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) {
        confirm("in requestfs");
 //       console.log("fileEntry is file?" + fileEntry.isFile.toString());
  //       fileEntry.name == 'someFile.txt'
  //      fileEntry.fullPath == '/someFile.txt'
        writeFile(fileEntry, null);

    }, fail);

}, fail);
function writeMyFile(fileEntry, dataObj) {
confirm("writemyfile begin");
    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function (fileWriter) {

        fileWriter.onwriteend = function() {
 //           console.log("Successful file write...");
            confirm("successful file write");
            readFile(fileEntry);
        };

        fileWriter.onerror = function (e) {
 //           console.log("Failed file write: " + e.toString());
            confirm("file write failed");
        };

        // 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);
        confirm("after fileWriter.write");
    });
}

Which gets passed the request for file system and no other confirms are reached.
Plus, I also tried:

function myWrite()
{
   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
confirm("after request filesystem");
        setTimeout(function(){
        nothing();
    },500);
   
readme.txt += "123467890";
    function gotFS(fileSystem) {
    confirm("in gotFS");
        var path = "readme.txt";

        fileSystem.root.getFile(path, {create: true, exclusive: false}, gotFileEntry, fail);
confirm("end of gotFS");
    }

    function gotFileEntry(fileEntry) {

        fileEntry.createWriter(gotFileWriter, fail);
confirm("gotfileentry");
    }

    function gotFileWriter(writer) {
        writer.onwrite = function(evt) {
            console.log("write success");
confirm("write Success");
        };
        writer.write("some sample text");
    }
}
which never reaches gotFS.
Any information will be helpful. thanks.

Kerri Shotts

unread,
Jul 25, 2016, 3:00:20 PM7/25/16
to phonegap
So, in your first example, you have:

        writeFile(fileEntry, null);

which is called when getFile succeeds. But then you later declare:

        function writeMyFile(fileEntry, dataObj) {

... which is not the same name. So my guess is the JS is failing with "undefined is not a function" in the first part.

Make the names match and try again; hopefully that will help you get further.

On Sunday, July 24, 2016 at 6:58:25 PM UTC-5, Doc Jump wrote:
...

Doc Jump

unread,
Jul 28, 2016, 2:04:57 PM7/28/16
to phonegap
fyi - I have found a site that is the best I've found at explaining how to use the filesystem with examples. It is here: http://www.tutorialspoint.com/cordova/cordova_file_system.htm.
The best part of this site is that all the examples, create, write and delete (didn't try read) work perfectly except for a couple of missing semi-colons. Regards.
Reply all
Reply to author
Forward
0 new messages