HTML5 File API修改file.name [英] HTML5 File API modify file.name

查看:94
本文介绍了HTML5 File API修改file.name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果发生什么事情,我正试图修改文件的名称。我试过做 file.name = file.name +'extra text'; 但它不起作用。我会如何去改变文件的名称一旦上传?

解决方案

我假设您使用HTML5 File API来存储沙盒文件到本地文件系统。
如果您想要修改存在文件的名称,您必须首先获取fileEntry对象:

  window.webkitRequestFileSystem(window函数(fs){
fs.root.getFile(targetFileFullName,{},function(fileEntry){
fileEntry.moveTo(original path,newName );
},errorHandler);
},onError);

FileEntry.moveTo函数可帮助您移动或重命名文件。你只是想重命名它,所以你所要做的就是给参数2分配新的名字,不要改变文件路径参数。

我写了一个 jsfiddl demo 显示本地存储文件的列表,目标名称字段表示您要将哪个文件修改和新的名称输入字段:



按下更改按钮后。 test3.txt文件将被修改:



希望这对您有所帮助。


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?

解决方案

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.

这篇关于HTML5 File API修改file.name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆