vue下载导出调用post接口
看了很多关于vue下载导出调用post接口和get请求的文章,在此记录一个本地有用的调用方法,也希望对一些小伙伴有用,直接上代码:
let url = '你的url'
axios.aaaaa.post(url,JSON.stringify(params),'queryEngine') // 这里是封装后的axios.aaaaa
.then(res => {
const xlsx = 'application/vnd.ms-excel'
const blob = new Blob([res.data], { type: xlsx })
//转换数据类型
const a = document.createElement('a') // 转换完成,创建一个a标签用于下载
a.download = `${this.$t('自定义文件名')}.xlsx`
a.href = window.URL.createObjectURL(blob)
a.click()
window.location.href = res.request.responseURL
})
亲测有效!
再加上一个get的:
import axios from "@/axios";
const url =
axios["testpo"].getRequestUrl("openUrl") + "?id=" + this.uniId;
window.open(url);
"testpo": {
getRequestUrl: (api)) => {
return "/gateway" + tempoAPI[api];
}
}