我在Chrome应用中使用Chrome文件系统,并且想重命名本地文件。我知道如何通过执行以下操作来写入新文件:

chrome.fileSystem.getWritableEntry(print_location, function(entry) {
    entry.getFile('file1.txt', {create:true}, function(entry) {
        entry.createWriter(function(writer) {
            writer.write(new Blob(['Lorem'], {type: 'text/plain'}));
        });
    });
});


哪个可行,但是给定一个已经存在的文件,如何重命名(覆盖)?或者,如何删除文件,复制文件或移动文件?这不可能吗?

更新:

基于Daniel Herr与HTML文件系统api共享的解释,我生成了以下代码,解决了我的问题。

function rename_file(file_location, file_old_name, file_new_name){
    chrome.fileSystem.getWritableEntry(file_location, function(entry) {
        entry.getFile(file_old_name, {create:false}, function(entry) {
            entry.moveTo(file_location, file_new_name,
                     function(){console.log("success");},
                     function(){
                        console.log("fail");
                      });
        });
    });
}

最佳答案

如果文件位于您有权访问的文件夹中,则可以使用entry.moveTo方法对其重命名。

fileentry.getParent(function(parent) {
 fileentry.moveTo(parent, "newname")
})


https://developer.mozilla.org/en-US/docs/Web/API/Entry#moveTo

关于javascript - 如何使用Filesystem API在Chrome应用中重命名文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37844713/

10-12 12:20
查看更多