问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/17560907/html5-file-api-modify-file-name