HTML5 File API modify file.name

喜你入骨 提交于 2020-01-02 05:40:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!