js点击下载图片
代码展示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片保存</title>
</head>
<body>
<img src="test.jpg" id="Download">
</body>
<script>
var Download = document.getElementById("Download");
// 保存文件函数
Download.onclick = function(){
var data = this.src;
console.log(data);
var filename='ercode';
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = data;
save_link.download = filename;
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
}
</script>
</html>