如何下载多张图片? [英] How to download multiple images?

查看:44
本文介绍了如何下载多张图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的代码中下载多张图片.我已经传递了单个URL进行下载,并且正在下载单个图像,但是当我将数组中的图像URL放入时,它们并没有被下载.这是我的代码:

I want to download multiple images in my code. I have passed single urls for download and the single image is getting downloaded but when i take image urls in an array, they are not getting downloaded. Here is my code:

 <script type="text/javascript">

 //var upics = ["https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0cq-7TkAONUjnDKz2AkEgiFRG6E0Dmrl43lOuj_nQCOrnQG8","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0cq-7TkAONUjnDKz2AkEgiFRG6E0Dmrl43lOuj_nQCOrnQG8"];

var _OBJECT_URL;
document.querySelector('#download-button').addEventListener('click', function() {
    var upics = ["https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQGvEiBfvtvDFi2aSNDydoH_DVaCJBtaaIpl0PhrddPdx4cwoAX","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0cq-7TkAONUjnDKz2AkEgiFRG6E0Dmrl43lOuj_nQCOrnQG8"];
    for (index = 0; index < upics.length; index++) {
        console.log("hiii");
    var request = new XMLHttpRequest();
    request.addEventListener('readystatechange', function(e) {
        /* if(request.readyState == 2 && request.status == 200) {
            document.querySelector('#start-download').style.display = 'block';
            document.querySelector('#download-button').style.display = 'none';
        }
        else if(request.readyState == 3) {
            document.querySelector('#download-progress-container').style.display = 'block';
            document.querySelector('#start-download').style.display = 'none';
        } */
         if(request.readyState == 4) {
            _OBJECT_URL = URL.createObjectURL(request.response);

            document.querySelector('#save-file').setAttribute('href', _OBJECT_URL);

            document.querySelector('#save-file').setAttribute('download', 'images.jpeg'); 
            /* document.querySelector('#save-file').setAttribute('download', [upics]); */
            document.querySelector('#save-file').style.display = 'block';
            document.querySelector('#download-progress-container').style.display = 'none';

            /* setTimeout(function() {
                window.URL.revokeObjectURL(_OBJECT_URL);

                document.querySelector('#download-button').style.display = 'block';
                document.querySelector('#save-file').style.display = 'none';
            }, 60*1000); */
        }
    });
   /*  request.addEventListener('progress', function(e) {
        var percent_complete = (e.loaded / e.total)*100;
        document.querySelector('#download-progress').style.width = percent_complete + '%';
    }); */
    request.responseType = 'blob';
    console.log("imageis"+upics[index]);
    /* request.open('get',[upics]); */
    //request.open('get', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0cq-7TkAONUjnDKz2AkEgiFRG6E0Dmrl43lOuj_nQCOrnQG8');
    request.open('get',upics[index]);
    request.send(); 
    }
});
</script>

推荐答案

您的循环正在覆盖 document.querySelector('#save-file').

尝试一下:

var upics = [
  "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQGvEiBfvtvDFi2aSNDydoH_DVaCJBtaaIpl0PhrddPdx4cwoAX",
  "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0cq-7TkAONUjnDKz2AkEgiFRG6E0Dmrl43lOuj_nQCOrnQG8"
];

//
function download(item) {
  var fileName = item.split(/(\\|\/)/g).pop();

  var image = new Image();
  image.crossOrigin = "anonymous";
  image.src = item;
  image.onload = function() {
  
    // use canvas to load image
    var canvas = document.createElement('canvas');
    canvas.width = this.naturalWidth;
    canvas.height = this.naturalHeight;
    canvas.getContext('2d').drawImage(this, 0, 0);
    
    // grab the blob url
    var blob;
    if (image.src.indexOf(".jpg") > -1) {
      blob = canvas.toDataURL("image/jpeg");
    } else if (image.src.indexOf(".png") > -1) {
      blob = canvas.toDataURL("image/png");
    } else if (image.src.indexOf(".gif") > -1) {
      blob = canvas.toDataURL("image/gif");
    } else {
      blob = canvas.toDataURL("image/png");
    }

    // create link, set href to blob
    var a = document.createElement('a');
    a.title = fileName;
    a.href = blob;
    a.style.display = 'none';
    a.setAttribute("download", fileName);
    a.setAttribute("target", "_blank");
    document.body.appendChild(a);
    
    // click item
    a.click();
  }
}

function downloadAll(item) {
  for (var i in upics) {
    download(upics[i]);
  }
}

<body>
  <button onclick="downloadAll()">  
  download  
</button>
</body>

这篇关于如何下载多张图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆