嗨,我正在尝试从网页下载图像。
在devtool中运行代码并为其创建手册。 我的代码正常工作,但是如果imgarr.length更大,我只会得到10或20个图像。我已经检查了所有请求,它们仍然可以工作(状态200)。请帮我解决这个问题。
var imgarr = [url1, url2,...] // using querySelectorAll to get Array of URL Image;
try {
for (var i = 0; i < imgarr.length; i++) {
download(imgarr[i],'image')
};
} catch (e) {
alert("Download failed.");
console.log('Download failed.', e);
}
function download(url, fileName) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.onreadystatechange = function () {
if(this.readyState == 4 && this.status == 200){
var url = window.URL.createObjectURL(this.response);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = fileName;
a.click();
document.body.removeChild(a);
}
}
xhr.send();
}