使用Chrome文件系统在用户选择的目录中创建文件



是否可以使用chrome.fileSystem在用户选择的目录中创建一个文件。是否类似于所选条目可以访问整个目录并可以执行创建、读取、删除操作?我列出了所选目录中的文件路径。

 chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(theEntry) {
                    if (!theEntry) {
                      output.textContent = 'No Directory selected.';
                      return;
                    }
                    // use local storage to retain access to this file
                    chrome.storage.local.set({'chosenResultDir': chrome.fileSystem.retainEntry(theEntry)});
                     ??? // writeNewFileTochosenResultDir(theEntry); // ?????
                  });

chrome.fileSystem.chooseEntry文档表示回调接收到一个Entry,在type: 'openDirectory'的情况下,它显然是一个DirectoryEntry,因此您可以使用File API创建一个文件:

chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(entry) {
    entry.getFile('newfilename.txt', {create: true}, function(file) {
        file.createWriter(function(writer) {
            writer.write(new Blob(['hello'])); // async
            writer.onwrite = function(e) {
                writer.onwrite = null;
                writer.truncate(writer.position); // in case we overwrite an exitsing file
                console.log('Done', e);
            };
        }, function(err) {
            console.error(err);
        });
    }, function(err) {
        console.error(err);
    });
});

manifest.json最低权限:

"permissions": [
    {"fileSystem": ["write", "directory"]}
],

官方Chrome示例应用程序存储库中提供了一个高级示例。

是的,您可以使用具有清单权限的web文件系统api完全访问所选文件夹的内容:{"filesystem":["write","directory"]}

最新更新