// imageSrc 下载图片的链接
// name 图片的名称
downloadImage (imageSrc, name) { let image = new Image()
// 告知请求的服务器 进行跨域请求 image.setAttribute(‘crossOrigin‘, ‘anonymous‘) image.src = imageSrc image.onload = function () { let canvas = document.createElement(‘canvas‘) canvas.width = image.width canvas.height = image.height console.log(‘canvas‘, canvas) let context = canvas.getContext(‘2d‘) context.drawImage(image, 0, 0, image.width, image.height) let url = canvas.toDataURL(‘image/png‘) let a = document.createElement(‘a‘) a.href = url
a.download = name || ‘photo‘ a.click() } }
原文:https://www.cnblogs.com/CoderZX/p/14868140.html