Changing the name of a file with FileWriter

194 views Asked by At

I wonder if it is possible to rename a file with FileWriter function. At this point, I can save my file without problem. But I'd like to change her name when saving. Here is my function:

function saveFile() {

    var s = fileEntry.name;
    var new_name = s.substring(0, s.lastIndexOf(".")) + ".min" + s.substring(s.lastIndexOf("."))

    fileEntry.name = new_name;

    fileEntry.createWriter(function(fileWriter) {
    fileWriter.onerror = function(e) {
        console.log("Write failed: " + e.toString());
    };

    var blob = new Blob([textarea.value]);
    fileWriter.truncate(blob.size);
    fileWriter.onwriteend = function() {
        fileWriter.onwriteend = function(e) {
            console.log("Write completed.");
        };

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

}

I guess it's possible, but I can not find how.

Thank you in advance!

1

There are 1 answers

0
Ben Wells On BEST ANSWER

To do this you need to use the DirectoryEntry API. To write to a new file you will need to use getFile to create a new file and write the contents into it.

To do this you also need to have access to the directory where you want to create the new file, and also permissions to create a file there. If the directory is within your sandboxed file system, this isn't a problem.

If you want to create the file on the user's file system it is more complicated. The user will need to have given your app access to the folder (via chrome.fileSystem.chooseEntry) and your app will need to have the chrome.fileSystem.directory and chrome.fileSystem.write permissions.