开发者

phonegap filewriter append issue

开发者 https://www.devze.com 2023-03-23 16:07 出处:网络
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.).

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(开发者_StackOverflow社区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.


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");
        }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消