赞
踩
- function save(n) {
- const canvas = document.getElementsByTagName('canvas')[0],
- // canvas的base64编码数据
- dataURL = canvas.toDataURL('image/png'),
- // 因为图片文件过大时,Blob就不会因为浏览器上下载失败
- src = dataURLtoBlob(dataURL),
- name = n + ".png";
- // IE
- if (window.navigator.msSaveOrOpenBlob) {
- navigator.msSaveBlob(src, name);
- } else {
- const link = document.createElement("a");
- link.setAttribute("href", window.URL.createObjectURL(src));
- link.setAttribute("download", name);
- document.body.appendChild(link);
- link.click();
- }
-
- function dataURLtoBlob(dataurl) {
- constarr = dataurl.split(','),
- mime = arr[0].match(/:(.*?);/)[1],
- byte = atob(arr[1]),
- n = byte .length,
- u8arr = new Uint8Array(n);
- while (n--) {
- u8arr[n] = byte .charCodeAt(n);
- }
- return new Blob([u8arr], { type: mime });
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。