user275157
user275157

Reputation: 1352

phonegap filewriter append issue

Trying to use phonegap 0.9.6 file storage. The code works fine while I am writing a file but does not work when I try to append to the file (using seek, truncate etc.).

There was a bug in version 0.9.5 that seems to be fixed.

The code just terminates when I call writer.seek. The alert after truncate (or if I delete the truncate the seek) is not being called at all.

Should I set an append flag somewhere? The doc says that but does not give an example as to where I should be setting the append flag. The code is as follows

function gotFS(fileSystem) {
        fileSystem.root.getFile( "test.txt", {"create":true,
            "exclusive":false}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }

    function gotFileWriter(writer) {
        writer.onwrite = function(evt) {
            console.log("write success");
        };

        writer.write("some sample text");
        // contents of file now 'some sample text'
        writer.truncate(11);
        alert('truncated');
        // contents of file now 'some sample'
        writer.seek(writer.length); //writer.seek(4) does not work either

        // contents of file still 'some sample' but file pointer is after the 'e' in 'some'
        writer.write(" different text");
        // contents of file now 'some different text'
        alert('success with diff text');
    }

Any help would be appreciated.

Upvotes: 4

Views: 2327

Answers (2)

Horst
Horst

Reputation: 11

The file handling is asynchron. You can't simply do the file operations step by step. You have to wait for the onwrite events, before making the next step. writer.abort() may fix the error. But you can't be sure what is stored.

Upvotes: 1

Titouan de Bailleul
Titouan de Bailleul

Reputation: 12959

I found a workaround to that :

        function gotFileWriter(writer) {
            writer.onwrite = function(evt) {
                console.log("write success");
            };

            writer.write("some sample text");
            // contents of file now 'some sample text'
            writer.abort();
            writer.truncate(11);
            // contents of file now 'some sample'
            writer.abort();
            writer.seek(writer.length); //writer.seek(4) does not work either

            // contents of file still 'some sample' but file pointer is after the 'e' in 'some'
            writer.write(" different text");
        }

Upvotes: 1

Related Questions