Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite a file in Chrome App?

I followed this example:

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.write(new Blob(['Lorem'], {type: 'text/plain'}));
            });
        });
        entry.getFile('file2.txt', {create:true}, function(entry) {
            entry.createWriter(function(writer) {
                writer.write(new Blob(['Ipsum'], {type: 'text/plain'}));
            });
        });
    });
});

to overwrite some existing file file1.txt and file2.txt.

But I found a problem: if the files are not empty, their content won't be fully overwritten, only the beginning part will be overwritten.

Do I need to remove the files first? Or do I miss something?

like image 254
Freewind Avatar asked Feb 25 '15 03:02

Freewind


People also ask

How do I overwrite an existing file?

Overwriting a File, Part 1 To edit the settings for a file, locate the file you wish to overwrite and hover over the file name. Click the chevron button that appears to the right of the file name and select Overwrite File from the menu.

How do I overwrite an existing file in Google Drive?

Right-click the file and select Manage versions. Click Upload new version and select a file from your computer. When the new version is done uploading, click Close.

How do you overwrite in Google Sheets?

Go to the add-on menu inside the Google Spreadsheet, choose Save Emails > Create Rule and check the option that says “Overwrite attachments”.


1 Answers

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'}));
            });
        });
    });
});
like image 88
Sarah Elan Avatar answered Sep 16 '22 11:09

Sarah Elan