js前端根据链接生成二维码并转成图片下载
依赖于jquery、jquery.qrcode.min.js
1.html
<div class="qrcode"></div>
<button onclick="downloadClick()">下载二维码</button>
2.js
2.1生成二维码
var url = window.location.href;
jQuery('.qrcode').qrcode(url);
2.2生成图片并下载
function downloadClick() {
var data = $("canvas")[0].toDataURL().replace("image/png", "image/octet-stream;"); //获取二维码值,并修改响应头部。
var filename = "load.png"; //保存的图片名称和格式
var saveLink= document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
saveLink.href = data;
saveLink.download = filename;
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
saveLink.dispatchEvent(event);
}