javascriptgoogle-chromehtml5-filesystempersistent-storage

How to overwrite file in FileSystem API


Here is the function:

this.saveObj = function(o, finished)
{   
    root.getDirectory("object", {create: true}, function(directoryEntry)
    {
        directoryEntry.getFile("object.json", {create: true}, function(fileEntry) 
        {
            fileEntry.createWriter(function(fileWriter) 
            {

                fileWriter.onwriteend = function(e) 
                {
                    finished(fileEntry);
                };

                fileWriter.onerror = errorHandler;
                var blob = new Blob([JSON.stringify(o)], {type: "json"});

                fileWriter.write(blob);
            }, errorHandler);
        }, errorHandler);
    }, errorHandler);
};

Now when I save an object everything works fine. Lets say I save {"id":1} my file content would be {"id":1}. Now I edit the object with o = {}; and save it again, my file content suddenly is {} "id":1 }.

It just overwrites the old content, but doesn't clean it. Do I have to delete the file before writing it or is there something I'm missing?


Solution

  • For as far as I understand the write method will write the supplied content to a position. To me this implies that the existing content is untouched unless you are overwriting parts. So I'm going to say yes, delete the file and save a new one.

    source