How to overwrite a file in Chrome App? How to overwrite a file in Chrome App? google-chrome google-chrome

How to overwrite a file in Chrome App?


It looks like write only overwrites the contents of the file at the specified position, so you are correct that if you want to completely replace the text of the file, you'd need to either remove the files first or truncate them.

This code worked for me, by truncating the file to the position of the writer after the write is finished.

chrome.fileSystem.chooseEntry({type:'openDirectory'}, function(entry) {    chrome.fileSystem.getWritableEntry(entry, function(entry) {        entry.getFile('file1.txt', {create:true}, function(entry) {            entry.createWriter(function(writer) {                writer.onwriteend = function(e) {                    e.currentTarget.truncate(e.currentTarget.position);                };                writer.write(new Blob(['Lorem'], {type: 'text/plain'}));            });        });        entry.getFile('file2.txt', {create:true}, function(entry) {            entry.createWriter(function(writer) {                writer.onwriteend = function(e) {                    e.currentTarget.truncate(e.currentTarget.position);                };                writer.write(new Blob(['Ipsum'], {type: 'text/plain'}));            });        });    });});