js点击按钮下载图片

322 阅读1分钟

功能是点击按钮下载图片资源到本地 之前代码是这种

save(){
    var alink = document.createElement('a');
    alink.href = this.codeSrc;
    alink.download = this.plateNo;
    alink.click();
}

但是在搜狗浏览器10版本下载不下来,之后换成这种方法

save(){
    const link = document.createElement('a');
    link.setAttribute('download', this.plateNo);

    link.href = this.getImageBase64(this.$refs.codeImg);
    link.click();
},
getImageBase64(image) {
    const canvas = document.createElement('canvas');
    canvas.width = image.width;
    canvas.height = image.height;
    const ctx = canvas.getContext('2d');
    ctx.drawImage(image, 0, 0, image.width, image.height);
    return canvas.toDataURL('image/', 1);
}

做个记录