4

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?

4
  • Do you need to save the file with the new name (server side)or r u trying to rename it using JS? Commented Jul 10, 2013 at 1:20
  • I just need to rename it using JavaScript. Commented Jul 10, 2013 at 3:22
  • You can't rename it. You can't change the file on a client's filesystem at all. Commented Jul 10, 2013 at 12:38
  • 1
    It isn't on just their system once they upload, I make a temporary copy in the browser to use. Commented Jul 10, 2013 at 17:53

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Which browsers support the filesystem API?
Chrome. Thats it for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.