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?
-
Do you need to save the file with the new name (server side)or r u trying to rename it using JS?Works On Mine– Works On Mine2013-07-10 01:20:36 +00:00Commented Jul 10, 2013 at 1:20
-
I just need to rename it using JavaScript.Ilan Biala– Ilan Biala2013-07-10 03:22:48 +00:00Commented Jul 10, 2013 at 3:22
-
You can't rename it. You can't change the file on a client's filesystem at all.Ray Nicholus– Ray Nicholus2013-07-10 12:38:30 +00:00Commented Jul 10, 2013 at 12:38
-
1It isn't on just their system once they upload, I make a temporary copy in the browser to use.Ilan Biala– Ilan Biala2013-07-10 17:53:56 +00:00Commented Jul 10, 2013 at 17:53
1 Answer
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:

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

Hope this is helpful for you.