5

1 回の操作で複数のファイルをディレクトリに保存しようとしています。chrome fileSystem api ドキュメントを正しく理解していれば、 chrome.fileSystem.chooseEntryにopenDirectoryオプションを使用すると、これが可能になるはずです。それも許されますか? ただし、ドキュメントは非常に最小限であり、Google で例を見つけることもできませんでした。

詳細な背景:
ディレクトリにアクセスするための適切な権限があり、書き込み権限もあります。

/*you need chrome >= Version 31.x [currently chrome beta]*/
"permissions": [
    {"fileSystem": ["write", "directory"]}, "storage", 
]

次に、chrome.fileSystem.chooseEntry(object options, function callback)chrome.fileSystem.getWritableEntry(entry entry, function callback)が残りますが、これらの関数が必要なものであるかどうかはわかりませんでした。

単一のファイルをファイル システムに保存する方法は次のとおりです。

chrome.fileSystem.chooseEntry({type:"saveFile", suggestedName:"image.jpg"}, 
    function(entry, array){
        save(entry, blob); /*the blob was provided earlier*/
    }
);

function save(fileEntry, content) {
    fileEntry.createWriter(function(fileWriter) {
        fileWriter.onwriteend = function(e) {
            fileWriter.onwriteend = null;
            fileWriter.truncate(content.size);
        };
        fileWriter.onerror = function(e) {
            console.log('Write failed: ' + e.toString());
        };
        var blob = new Blob([content], {'type': 'image/jpeg'});
        fileWriter.write(blob);
    }, errorHandler);
}

しかし、 chrome.fileSystem.chooseEntry({type:"openDirectory",..}を使用する場合、またはopenDirectoryは読み取り権限のみを付与する場合、複数のファイルを保存するにはどうすればよいですか?

4

1 に答える 1

8

私はこれがうまくいくと信じています。

chrome.fileSystem.chooseEntry({type:'openDirectory'}, function(entry) {
    chrome.fileSystem.getWritableEntry(entry, function(entry) {
        entry.getFile('file1.txt', {create:true}, function(entry) {
            entry.createWriter(function(writer) {
                writer.write(new Blob(['Lorem'], {type: 'text/plain'}));
            });
        });
        entry.getFile('file2.txt', {create:true}, function(entry) {
            entry.createWriter(function(writer) {
                writer.write(new Blob(['Ipsum'], {type: 'text/plain'}));
            });
        });
    });
});
于 2013-11-03T23:00:47.557 に答える