下载模板是二进制流时,采用以下方式进行下载;
点击按钮后创建一个a标签,实现自动下载。
function downLoadByBlob (url, params, title = "模版") {
return axios({
url: url,
method: 'get',
params: params,
responseType: 'blob', // 指定获取数据的类型为blob
paramsSerializer: function (params) {
return Qs.stringify(params, { arrayFormat: 'repeat' })
}
}).then(res => {
const link = document.createElement('a')
const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.setAttribute('download', title)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
}
使用方法:
downLoadByBlob(downloadUrl, this.queryParam, '文件名称.xlsx')