vue - 文件流下载

//文件流下载
import axios from 'axios';
// 导出全部
        download() {
            this.btnloading = true;
            axios.get('/admin/annex/guwencard/downloadxcx', { params: { lpid: this.lpid } }).then((res) => {
                if (res.status == 200) {
                    // 利用a标签自定义下载文件名
                    const link = document.createElement('a');
                    // 创建Blob对象,设置文件类型
                    let blob = new Blob([res.data.data.url], {
                        type: 'application/zip',//MIME类型
                    });
                    link.href = URL.createObjectURL(blob); // 创建URL
                    link.setAttribute('download', this.lpname + '.zip'); // 设置下载文件名称
                    link.click(); // 下载文件
                    document.body.appendChild(link);
                    URL.revokeObjectURL(link.href); // 释放内存
                    this.btnloading = false;
                } else {
                    this.$message.error(res.statusText);
                    this.btnloading = false;
                }
            });
        },

//普通下载
                   const link = document.createElement('a');
                    link.href = downpath;
                    link.setAttribute('download', this.lpname + '.zip'); // 设置下载文件名称
                    link.click(); // 下载文件
posted @ 2021-01-05 10:20  gggggggxin  阅读(2112)  评论(0编辑  收藏  举报