2013-11-03 42 views
5

我試圖將多個文件保存到一個目錄 - 在一個操作中。如果我正確理解chrome fileSystem api documentation這應該是可能的,當我使用openDirectory選項chrome.fileSystem.chooseEntry。這甚至允許嗎?
但是,文檔非常簡約,我也沒有通過谷歌找到任何示例。如何在Chrome應用程序中保存多個文件

更多的背景:
我有適當的權限來訪問目錄,也有寫權限:

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

然後你留下了chrome.fileSystem.chooseEntry(對象的選項,函數回調)chrome.fileSystem.getWritableEntry(入口條目,函數回調),但我沒有弄清楚這些函數是否甚至是我想要的。

這裏是如何單個文件可以保存到文件系統:

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({類型:「openDirectory」 ,..}或不openDirectory只給我讀權限?

+0

後續http://stackoverflow.com/q/28710804/632951 – Pacerier

回答

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'})); 
      }); 
     }); 
    }); 
}); 
+0

**哇**。我無法相信這是有效的,教程對此完全默默無聞。你怎麼知道這件事的? – Pacerier

相關問題