JS下载图片

<body>
    <button id="button" type="button">下载</button>
</body>
<script>
    var a = document.getElementById("button");
    a.addEventListener("click", function () {
        for (let i = 1; i < 215; i++) {//299
		var url =	"http://www/"+ i +".jpg?210";//215"
	 
  
            var xmlhttp;
            xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", url, true);
            xmlhttp.responseType = "blob"; // 请求返回的数据类型
            xmlhttp.onload = function () {
                if (this.status == 200) {
                    var blob = this.response;
                    var img = document.createElement("img"); // 预览图片
                    img.onload = function (e) {
                        window.URL.revokeObjectURL(img.src);
                    };
                    img.src = window.URL.createObjectURL(blob);
                    document.body.appendChild(img);
                    var a = document.createElement('a'); // 下载图片
                    a.href = window.URL.createObjectURL(blob); //图片地址
                    a.download = i + '.jpg'; // 图片名字
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
					//setTimeout(() => console.log(i), 1000) 
                }
            }
            xmlhttp.send();
			//setTimeout(() => console.log(i), 1000) 
        }
    })
</script>

  火狐可以下载,谷歌下载容易漏文件。没找到 问题

原文地址:https://www.cnblogs.com/jksun/p/15010621.html