文件流:
通过下面代码下载文件后
api:
export const downloadExcel = (params) => {
return request({
url: '/api/mc/*******/export',
method: 'get',
params: params
})
}
js:
handleDownload2() {
var params = Object.assign({},this.query)
downloadExcel(params).then((res)=>{
console.log(res)
this.downfile('乡镇船舶基础信息表', res.data)
})
// window.open(`/api/mc/townshipshipinfo/export?${this.website.tokenHeader}=${getToken()}&shipName=${this.query.shipName}&shipDistrict=${this.query.shipDistrict}&shipOwnerName=${this.query.shipOwnerName}&portId=${this.query.portId}&moorage=${this.query.moorage}`);
},
downfile (name, data) {
let fileName = name
let blob = new Blob([data], { type: 'application/vnd.ms-excel;charset=UTF-8' })
const a = document.createElement('a')
// 创建URL
const blobUrl = window.URL.createObjectURL(blob)
a.download = fileName
a.href = blobUrl
document.body.appendChild(a)
// 下载文件
a.click()
// 释放内存
URL.revokeObjectURL(blobUrl)
document.body.removeChild(a)
},
原因:
请求数据的时候没有加请求数据类型,导致请求到的数据错误
正确的是
请求接口 responseType: 'blob',
api:
export const downloadExcel = (params) => {
return request({
url: '/api/mc/*******/export',
method: 'get',
responseType: 'blob',
params: params
})
}
正确的数据类型: