前端下载图片 在浏览器中

这篇文章很好:https://blog.csdn.net/weixin_44244857/article/details/105549543

 

使用了文章文中base64 方法

<el-button size="small" @click="exportDatas(scope.row.img)" type="info" plain>下载</el-button>

 //下载
    exportDatas: function (img) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', img);
        xhr.responseType = 'blob';
        xhr.send();
        xhr.onload = function () {
            if (this.status === 200 || this.status === 304) {
                const fileReader = new FileReader();
                fileReader.readAsDataURL(this.response);
                fileReader.onload = function () {
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.href = this.result;
                    a.download = '处方单';
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                };
            }
        };
    },