2017-02-26 111 views
1

我知道,我可以使用:Chrome擴展:文件保存到擴展目錄

chrome.downloads.download({ 
    url: 「http://your.url/to/download」, 
    filename: 「suggested/filename/with/relative.path」 // Optional 
}); 

一些文件保存到用戶指定的位置。有沒有辦法讓該位置成爲擴展文件夾的一部分?

幾個可能的使用情況:

  • 也許我想允許用戶保存一個新的圖標以用於 擴展。
  • 也許我想讓用戶只需將文件保存到 即可使用擴展名,而不用擔心手動刪除那些文件爲 。

回答

2

您可以使用沙盒文件系統API:https://www.html5rocks.com/en/tutorials/file/filesystem/

首先,添加 「unlimitedStorage」 權限清單。

然後,訪問文件系統,寫入文件,並通過文件系統訪問它們的URL

webkitRequestFileSystem(PERSISTENT, 1024, function(filesystem) { 
filesystem.root.getFile("test", { create: true }, function(file) { 
    file.createWriter(function(writer) { 
    writer.addEventListener("write", function(event) { 
    location = file.toURL() 
    }) 
    writer.addEventListener("error", console.error) 
    writer.write(new Blob([ "test" ])) 
    }, console.error) 
}, console.error) 
}, console.error)