jszip创建图像的空txt文件并将其压缩

时间:2018-07-02 20:49:38

标签: javascript jquery jszip

我正在使用此代码下载这些图像,但是此程序制作了具有这些名称和类型jpeg的txt文件。为什么会这样呢?由于跨站点,此程序无法在chrome上运行,但在zip文件中的firefox上为空txt文件。

<!DOCTYPE html>
<html>
<body>
  <p id="demo"></p>
  <script type="text/javascript" src="https://fastcdn.org/FileSaver.js/1.1.20151003/FileSaver.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js" type="text/javascript">
  </script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip-utils/0.0.2/jszip-utils.min.js" type="text/javascript">
  </script>
  <script>
    var urls = [
      "https://s3.amazonaws.com/ais-django/Events/Test1/DSC_0397.jpg",
      "https://s3.amazonaws.com/ais-django/Events/Test1/DSC_0398.jpg",
      "https://s3.amazonaws.com/ais-django/Events/Test1/DSC_0488.jpg"
    ];
    var nombre = "Zip_img";
    //The function is called
    compressed_img(urls, nombre);

    function compressed_img(urls, nombre) {
      var zip = new JSZip();
      var count = 0;
      var name = nombre + ".zip";
      urls.forEach(function(url) {
        JSZipUtils.getBinaryContent(url, function(err, data) {
          if (err) {
            throw err;
          }
          zip.file(url, data, {
            binary: true
          });
          count++;
          if (count == urls.length) {
            zip.generateAsync({
              type: 'blob'
            }).then(function(content) {
              saveAs(content, name);
            });
          }
        });
      });
    }
  </script>
</body>
</html>

0 个答案:

没有答案