文件流下载文件(xls,pdf,xlsx,zip)
/**
* @param {*} res 数据 二进制流
* @param {*} fileName 文件名
*/
export const exportFile = (res, fileName = 'fileData', fileType = 'xlsx') => {
let url = ''
let blobTypeParams = {}
if (fileType === 'xls') {
blobTypeParams = {
type: "application/vnd.ms-excel"
}
}
if (fileType === 'pdf') {
blobTypeParams = {
type: "application/pdf"
}
}
if (fileType === 'xlsx') {
blobTypeParams = {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}
}
if (fileType === 'zip') {
blobTypeParams = {
type: "application/zip"
}
}
url = window.URL.createObjectURL(new Blob([res]), blobTypeParams)
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', `${fileName}.${fileType}`)
document.body.appendChild(link)
link.click()
}
axios下载文件流
responseType 默认blob ,arrayBuffer
export const download = async (url, data, method = 'post', responseType = 'blob') => {
const res = await axios({ method, url, data, responseType })
return Promise.resolve(res.data)
}