当前位置:网站首页>JS to download files in batches
JS to download files in batches
2022-06-13 08:19:00 【Parthenocissus still works】
Realize batch downloading of data list details :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Bulk download </title>
</head>
<body>
<button class="hula"> Bulk download </button>
<a href="http://localtest.com/img/01.jpg" target="_blank" download="01.jpg" class="muldown">down1</a>
<a href="http://localtest.com/img/02.jpg" target="_blank" download="02.jpg" class="muldown">down2</a>
<a href="http://localtest.com/img/03.jpg" target="_blank" download="03.jpg" class="muldown">down3</a>
</body>
</html>
<script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('.hula').click(function(){
// Method 1 :
/*let downbtns = $('.muldown');
for (let x in downbtns) {
// You can choose to use setTimeout, Scheduled batch trigger downloading
$(downbtns[x])[0].click();
}*/
// Method 2 : Applicable interface ajax Request bulk download
let downHrefs = ["http://localtest.com/img/01.jpg", "http://localtest.com/img/02.jpg","http://localtest.com/img/03.jpg"];
for(let i=0; i< downHrefs.length; i++){
// window.open(downHrefs[i]);// You can directly access the download address , have access to window.open()
var currUrl = downHrefs[i].split("/");
var tarName = currUrl[currUrl.length - 1];
console.log(tarName);
downloadIamge(downHrefs[i], tarName);
}
});
})
function downloadIamge(imgsrc, name) {
// Download picture address and picture name
var image = new Image();
// To solve the cross domain Canvas pollution problem
image.setAttribute('crossOrigin', 'anonymous');
image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0, image.width, image.height);
var _dataURL = canvas.toDataURL('image/png'); // photographic base64 Encoding data
var blob_ = dataURLtoBlob(_dataURL); // be used Blob Because the picture file is too large , The download will fail on a wind browser , and Blob Won't
var url = {
name: name || " picture .png", // There is no need to add... To the picture name .png Suffix name
src: blob_
};
if (window.navigator.msSaveOrOpenBlob) { // if browser is IE
navigator.msSaveBlob(url.src, url.name);//filename The file name includes the extension , The download path is the browser default path
} else {
var link = document.createElement("a");
link.setAttribute("href", window.URL.createObjectURL(url.src));
link.setAttribute("download", url.name + '.png');
document.body.appendChild(link);
link.click();
}
};
image.src = imgsrc;
function dataURLtoBlob(dataurl) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type: mime});
}
}
</script>
边栏推荐
- ES6 deleting an attribute of an object
- The method of SolidWorks modifying text font in engineering drawing
- Cosmos star module development
- 【博弈论-完全信息静态博弈】 Nash均衡
- Cosmos Starport installation and startup
- 酒水批发行业应当如何高效管理商品与库存
- [virt-manager]远程管理启动虚拟机时,鼠标飘逸的问题
- 直播回顾 | 积极防御体系下BAS技术创新探索
- [complete information static game characteristics of Nash equilibrium]
- [problem record] json decoder. JSONDecodeError:Extra data: line xxx column xxx(char xxxx)
猜你喜欢
The way of distributed system: Lamport logical clock
Using KVM to create three virtual machines that can communicate with local area network
Recognition of COVID-19 based on paddlepaddle
How to use annotations in word
学习记录4: einops // cudnn.benchamark=true // hook
[game theory complete information static game] Application of Nash equilibrium
Dfinity (ICP) basic development tutorial-5
Microservice project construction III: automatic code generation
基于paddlepaddle的新冠肺炎识别
ERP基础数据 华夏
随机推荐
4. fabric2.2 create and join channels (use the official demo)
[virt manager] remote management the problem of the floating mouse when starting the virtual machine
HCIP_ Static experiment
批发商为什么要使用订单系统
Is there any good management software to solve the problems faced by tea wholesalers
杨氏矩阵查找数字是否存在
How to efficiently manage commodities and inventory in the beverage wholesale industry
Redis subscribe connection timeout interrupt problem solution
Plane merging (matlab)
How to hide tabs in nailing applet
Microservice project construction III: automatic code generation
es6删除对象的某个属性
Methods of importing and exporting settings in Altium Designer
[complete information static game characteristics of Nash equilibrium]
Cosmos star application case
Dfinity (ICP) basic development tutorial-5
[game theory complete information static game] Nash equilibrium
Penetration problem (main directory, password explosion, database uploading Trojan horse)
从零开始-实现JpetStore网站-1-建立项目框架以及项目介绍
How does the BD new tab plug-in log in?