<button type="button" onclick="imgDownLoad()">点击下载</button>
function downloadImg(url, name) {
fetch(url)
.then(function (response) {
return response.blob();
})
.then(function (response) {
const a = document.createElement("a");
a.style.display = "none";
a.href = URL.createObjectURL(response);
a.download = name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
});
}
function imgDownLoad() {
let url =
"https://tse1-mm.cn.bing.net/th/id/R-C.2f2858f7d8b57a848633cc02711a4a4f?rik=LeMvCvdwReQy9g&riu=http%3a%2f%2ftupian.sioe.cn%2fb%2fbing-home-image%2fpic%2f20140729.jpg&ehk=12NQ%2f5iVQ%2ffbeZWHB9ryq2tAyCh9hDZDCkHe7eAqVQw%3d&risl=&pid=ImgRaw&r=0";
let name = "文件名称";
downloadImg(url, name);
}