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>