如何使用Filesystem API重命名Chrome应用程序中的文件?

时间:2021-09-01 10:48:01

I'm using the Chrome Filesystem in my Chrome app and I want to rename a local file. I know how to write a new file, by doing:

我在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'}));
        });
    });
});

which works, but given a file that already exists, how do I rename it (with overwrite)? Or alternatively how can I delete files, copy files, or move files? Is this not possible?

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

UPDATE:

Based on Daniel Herr's explanation that it's shared with HTML file system api, I produced the below code, which solved my issue.

基于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"); 
                      });
        });
    });
}

1 个解决方案

#1


2  

If the file is inside a folder you have been given access to, you can use the entry.moveTo method to rename it.

如果文件位于您已被授予访问权限的文件夹中,则可以使用entry.moveTo方法重命名该文件。

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

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

#1


2  

If the file is inside a folder you have been given access to, you can use the entry.moveTo method to rename it.

如果文件位于您已被授予访问权限的文件夹中,则可以使用entry.moveTo方法重命名该文件。

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

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