vue+ts+elementUI:后端返回文件流,前端下载pdf文件到本地

vue+ts+elementUI:后端返回文件流,前端下载pdf文件到本地

1.配置接口

//下载天气信息pdf
export const downWeatherPdfService = (paramsData: any) => {
    const res: any = service.post('/weaapi/api/dispatch/weather_pdf', paramsData,{responseType: 'blob'});
    return res;
}

2.js方法

 async downWeatherInfoPdf() {
        let parms = {
            FILE_INFO: this.weatherTitleInfo
        }
        const self = this;
        const resData = (await downWeatherPdfService(parms)).data;

        if (resData) {
            const blob = new Blob([resData], { type: "application/pdf" })
            const fileName = this.weatherTitleInfo.replace(/-/g,"").replace(" ","_") + ".pdf";
            let url = window.URL.createObjectURL(blob);
            let link = document.createElement('a');
            link.style.display = 'none';
            link.href = url;
            link.setAttribute('download', fileName);
            document.body.appendChild(link);
            link.click();
        } else {
            this.$message.warning('下载失败');
        }
    }

3.html调用方法

<el-button type="info" size="small" class="right-btn" @click="downWeatherInfoPdf()">保存</el-button>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值