vue实现post请求,下载后端返回文件流
·
get请求,浏览器可以直接下载后台返回的文件流,但是post请求,浏览器不会直接下载。
直接贴代码:
myPlanEdit(type) {
this.$http.defaults.headers.common['token'] = this.token;
this.save_type = type
let datas = {
'id':this.planId,
'name':this.dataObj.name,
'introduce':this.dataObj.introduce,
'product_ids':this.product_ids,
'save_type':this.save_type,
'solution_icon_id':this.dataObj.icon_attachment.id,
}
let file_type = ''
if(type == '0'){
file_type = 'application/msdoc;charset=UTF-8'
}
if(type == '1'){
file_type = 'application/pdf;charset=UTF-8'
}
this.$http.post(URL,datas,{responseType: 'arraybuffer'})
.then(res => {
let pdfUrl = window.URL.createObjectURL(new Blob([res.data],{type: file_type}));
let temp = res.headers["content-disposition"].split(";")[1].split("filename=")[1];
let s= /\"/g
temp = temp.replace(s,'')
let iconv = require('iconv-lite');
iconv.skipDecodeWarning = true;//忽略警告
temp = iconv.decode(temp, 'gbk');
const link = document.createElement('a');
link.href = pdfUrl;
link.setAttribute('download', temp);
document.body.appendChild(link);
link.click()
document.body.removeChild(link)
}).catch(error => {
console.log(error)
})
},
代码截图
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)