我正在尝试将多个文件保存到一个目录中 - 在一次操作中。如果我正确理解 chrome fileSystem api documentation当我为 chrome.fileSystem.chooseEntry 使用 openDirectory 选项时,这应该是可能的。这甚至被允许吗?
但是,文档非常简单,我也没有通过谷歌找到任何示例。
更多背景:
我有访问目录的适当权限,也有写入权限:
/*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 只允许我读取时,我如何保存多个文件-权利?
最佳答案
我相信这应该有效。
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'}));
});
});
});
});
关于javascript - 如何在 Chrome 应用程序中保存多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759200/