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

Create file within in the user selected directory using Chrome filesystem

是否可以使用 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); // ?????
                  });

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

chrome.fileSystem.chooseEntry documentation says the callback receives an Entry, which in case of type: 'openDirectory' is obviously a DirectoryEntry, so you can use 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 sample app repository