3

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

According to the Mozilla documentation using only { create: true} :

The existing file or directory is removed and replaced with a new one, then the successCallback is called with a FileSystemFileEntry or a FileSystemDirectoryEntry, as appropriate.

Tested in Chrome 72 this seems to be the case.

This does not work as the file seems to be persist. The file will be overwritten (first bytes) but the size will remain the same. So this is a bug in at least Chrome 72.

Source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.