titocalata
titocalata

Reputation: 45

Phonegap FileWrite overwrite file

I'm trying to create an APP with phonegap but I having an issue with FileWriter. I'm creating a file were the user will be including data. When the user complete for the first time a form, is created a ".txt" document with the data. The problem, is when complete again the form with different data, instead to write after the first text, overwrite the document.

I read in the phonegap document:

By default, the FileWriter writes to the beginning of the file, overwriting existing data. Set the optional append boolean to true in the FileWriter's constructor to write to the end of the file.

And I don't have any idea in how to do that.... I leave part of my code in case it helps:

function writeXML(name, data){

            .createWriter(function gotFileWriter(writer) {

                fileWriter = writer;
                console.log("Getting writer...");

             }, fail);
        }, fail);

    while(fileWriter==null){
        //console.log("Waiting for file writer...");
    }
    fileWriter.onwriteend = function(evt) {
    console.log("File saved");
                            };
    fileWriter.write(data);}

Thanks a lot!

Upvotes: 2

Views: 2826

Answers (1)

titocalata
titocalata

Reputation: 45

Just in case someone needed:

I just add

writer.seek(writer.length);

after:

fileEntry.createWriter(function gotFileWriter(writer) {

And it's working :D

Upvotes: 2

Related Questions