Ilan Biala
Ilan Biala

Reputation: 3417

HTML5 File API modify file.name

I am trying to modify a file's name if something happens. I have tried doing file.name = file.name + 'extra text'; but it doesn't work. How would I go about changing the file's name once it is uploaded?

Upvotes: 4

Views: 7128

Answers (1)

Chickenrice
Chickenrice

Reputation: 5727

I assume that you are using HTML5 File API to store sandboxed file to local file system. You have to get fileEntry object first if you want to modify an exist file's name:

window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs){
    fs.root.getFile("targetFileFullName",{},function(fileEntry){
        fileEntry.moveTo("original path","newName");
    },errorHandler);
}, onError);

FileEntry.moveTo function help you move or rename file. You just want rename it so all you have to do is assign new name to parameter two and do not change file path parameter.

I wrote a jsfiddl demo that show a list of your local storage files and a target name field means which file you want to modify and a new name input field:

enter image description here

After you press the change button. The "test3.txt" file will be modify:

enter image description here

Hope this is helpful for you.

Upvotes: 2

Related Questions