function?downloadImage(src)?{
??const?a?=?document.createElement('a')
??const?urlArr?=?src.split('/')
??const?name?=?urlArr[urlArr.length?-?1]
??a.download?=?name
??a.style.display?=?'none'
??const?image?=?new?Image()
??image.crossOrigin?=?"Anonymous"
??image.src?=?src?+?'?v='?+?Math.random()
??image.onload?=?()?=>?{
????const?base64?=?getBase64Image(image)
????a.href?=?base64
????document.body.appendChild(a)
????a.click()
????document.body.removeChild(a)
??}
??const?getBase64Image?=?(img)?=>?{
????const?canvas?=?document.createElement("canvas");
????canvas.width?=?img.width;
????canvas.height?=?img.height;
????const?ctx?=?canvas.getContext("2d");
????ctx.drawImage(img,?0,?0,?img.width,?img.height);
????const?ext?=?img.src.substring(img.src.lastIndexOf(".")?+?1).toLowerCase();
????const?dataURL?=?canvas.toDataURL("image/"?+?ext);
????return?dataURL;
??}
}
通过以上方式下载https://img.3dmgame.com/uploa...
会出现跨域错误,
为什么图片可以另存为呢?