【问题标题】:Downloading multiple files and zip - Chrome extension下载多个文件和 zip - Chrome 扩展
【发布时间】:2012-12-20 06:18:40
【问题描述】:

是否可以将多个图像下载到沙盒文件系统中(没有“另存为”对话框,或最多一个另存为对话框)?

下载后,我想把它们压缩成一个.. 有没有 javascript 存档库?

提前谢谢..

【问题讨论】:

  • 您可以访问页面中的所有contents of resources(图像、js\css 文件等)我不确定是否通过 JS 归档

标签: javascript google-chrome-extension download archive


【解决方案1】:

您可以为此使用zip.js。 它已经有 API 用于从 HTTP 获取要压缩的内容(参见 zip.HttpReader 构造函数)和在 HTML5 文件系统上编写生成的 zip(参见 zip.FileWriter 构造函数)。

这是一个使用filesystem API的示例:

index.html文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Zip JSON data from the BBC into HTML5 FileSystem</title>
</head>
<body>
  <script src="zip.js"></script>
  <script src="zip-fs.js"></script>
  <script src="zip-ext.js"></script>
  <script src="example.js"></script>
</body>
</html>

example.js文件:

// create a zip virtual filesystem
var fs = new zip.fs.FS();

// add some files into the zip filesystem 

// add the "bbc-music.json" file in the root directory
fs.root.addHttpContent("bbc-music.json", 
  "http://www.bbc.co.uk/programmes/genres/music.json");
// add the "bbc-learning.json" file in the root directory
fs.root.addHttpContent("bbc-learning.json", 
  "http://www.bbc.co.uk/programmes/genres/learning.json");

// create a file named "test.zip" in the root directory of the HTML5 filesystem 
createFile("test.zip", function(fileEntry) {
  // export the zip content into "test.zip" file
  fs.root.exportFileEntry(fileEntry, function() {
    console.log("done");
  });
});

// function to create a file in the HTML5 temporary filesystem
function createFile(filename, callback) {
  webkitRequestFileSystem(TEMPORARY, 4 * 1024 * 1024, function(fs) {
    fs.root.getFile(filename, { create : true }, callback);
  });
}

【讨论】:

  • Chrome 是 Chrome 扩展程序通常运行的地方。
  • 这是旧的,但对于我来说,我无法弄清楚如何下载创建的 zip。我已经创建工作,但我不确定如何找到创建文件的 URL。
【解决方案2】:

您可以通过XMLHTTPRequests 将图像作为某些网页的常规部分访问或单独下载。在此之后,您可以使用 JSZip JavaScript 库将它们压缩到一个存档中。 zip 可以存储为没有“另存为”对话框的文件(尝试网站上的示例)。虽然我不确定你为什么需要sandbox

还有其他用于压缩的 JavaScript 库,例如,other SO answer 中提到了一些。

【讨论】:

  • 没有。不是ZIP ..问题实际上是-当我下载图像时,我可以在没有“另存为”对话框的情况下下载图像吗?但是站点中的示例看起来比我想象的要简单...我会先尝试一下
猜你喜欢
  • 2017-02-07
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多