1、nginx配置解决缓存问题
if ($request_filename ~* .*\.(?:htm|html|json)$) {
add_header Cache-Control "no-store";
}
2、流转化问题 , 注意点: responseType: 'blob' 或者 responseType: 'arraybuffer' 必须加上,如果有使用Mock模拟数据,会影响导致文件损坏,最好是不要使用mock。(mock重写了原生请求,导致和blob流数据冲突,并且导致promise finally失效)
// window.open(`/cccccc.ajax}`) // 不需要鉴权直接window.open 或者 location.href
http.get(`/cccccc.ajax`, {}, {
responseType: 'blob'
}).then((response) => {
let name = new Date().getTime() + '_支付单.xlsx'
let blob = new Blob([response.data], {type: 'application/vnd.ms-excel'})
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, name)
} else {
const link = document.createElement('a')
link.style.display = 'none'
link.href = URL.createObjectURL(blob)// 创建url对象
link.download = name
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
URL.revokeObjectURL(link.href)// 销毁url对象
}
}).catch((response) => {
console.error(response.message)
})
3、下载pdf
function dowanloadpdf() {
axios({
method: 'get',
url:
'https://v.pdf',
responseType: 'blob',
}).then(function (response) {
const link = document.createElement('a')
let blob = new Blob([response.data], { type: response.data.type })
let url = URL.createObjectURL(blob)
link.href = url
link.download = '前端工程师必备技能.pdf'
link.click()
})
}