1.发请求:
order_ids=[]
try {
const response = await request.post(
url,
{
order_ids: order_ids,
},
{
responseType: 'blob',
}
)
if (response.status === 201) {
//获取文件名
const filename =
response.headers['content-disposition'].split('filename=')[1]
this.handleDownload(response.data, filename)
}
} catch (error) {
console.error('下载失败:', error)
}
2.下载
handleDownload(content: any, filename: string) {
const blob = new Blob([content], { type: 'application/zip' })
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.style.display = 'none'
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
window.URL.revokeObjectURL(url)
document.body.removeChild(a)
}
这个是必须的responseType: 'blob',当缺失时下载的zip文件打开会出现这种情况