JSZIP скачать файл с http пути - PullRequest
0 голосов
/ 11 сентября 2018

Я пытаюсь загрузить images с url моего локального хоста

Мой код здесь

<head>
  <script src="Stuk-jszip-9fb481a/dist/jszip.js"></script>
  <script>
    window.onload = function() {
      var zip = new JSZip();
      var a = document.querySelector("a");
      var urls = ["http://localhost/cce/assests/images//save_img.png"];

      function request(url) {
        return new Promise(function(resolve) {
          var httpRequest = new XMLHttpRequest();
          httpRequest.open("GET", url);
          httpRequest.onload = function() {
            zip.file(url, this.responseText);
            resolve()
          }
          httpRequest.send()
        })
      }

      Promise.all(urls.map(function(url) {
          return request(url)
        }))
        .then(function() {
          console.log(zip);
          zip.generateAsync({
              type: "blob"
          })
          .then(function(content) {
            a.download = "folder" + new Date().getTime();
            a.href = URL.createObjectURL(content);
            a.innerHTML = "download " + a.download;
          });
        })
    }
  </script>
</head>

<body>
  <a href="" download>download</a>
</body>

но вместо него создается папка http для хранения изображения

отображается на консоли, как

http:/
:
ZipObject {name: "http:/", dir: true, date: Tue Sep 11 2018 18:39:38 GMT+0530 (India Standard Time), comment: null, unixPermissions: null, …}
http://localhost/
:
ZipObject {name: "http://localhost/", dir: true, date: Tue Sep 11 2018 18:39:38 GMT+0530 (India Standard Time), comment: null, unixPermissions: null, …}

1 Ответ

0 голосов
/ 11 сентября 2018

Попробуйте этот код

<head>
  <script src="Stuk-jszip-9fb481a/dist/jszip.js"></script>
  <script>
    window.onload = function() {
      var zip = new JSZip();
      var a = document.querySelector("a");
      var urls = ["http://localhost/cce/assests/images//save_img.png"];

    const toDataURL = url => fetch(url)
      .then(response => response.blob())
      .then(blob => new Promise((resolve, reject) => {
        const reader = new FileReader()
        reader.onloadend = () => resolve(reader.result)
        reader.onerror = reject
        reader.readAsDataURL(blob)
      }));


      function request(url) {
        return new Promise(function(resolve) {
          toDataURL(url)
              .then(dataUrl => {
                zip.file("image.png", dataUrl.substr(dataUrl.indexOf(',')+1), {base64: true});
                resolve()
              });
              })
      }

      Promise.all(urls.map(function(url) {
          return request(url)
        }))
        .then(function() {
          console.log(zip);
          zip.generateAsync({
              type: "blob"
          })
          .then(function(content) {
            a.download = "folder" + new Date().getTime();
            a.href = URL.createObjectURL(content);
            a.innerHTML = "download " + a.download;
          });

        })

    }
  </script>
</head>
...