接口请求的时候加上响应类型
request.responseType = 'blob'
接收数据处理
fileDownload (content, name = new Date().valueOf() + '登录日志', suffix = 'xlsx') {
const blob = new Blob([content], { type: 'application/octet-stream' })
const downloadElement = document.createElement('a')
const href = window.URL.createObjectURL(blob)
downloadElement.href = href
downloadElement.download = `${name}.${suffix}`
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement)
window.URL.revokeObjectURL(href)
},
下载文件
export const downloadFile = (blob: Blob, downloadName: string, type: string = 'xls', defDate: boolean = true) => {
let blobData: Blob = blob
if (type === 'zip') {
blobData = new Blob([blob], {
type: 'application/zip',
});
}
const url = window.URL.createObjectURL(blobData);
const link = document.createElement('a');
link.style.display = 'none';
link.href = url;
const name = downloadName + (defDate ? `_${formatTime(new Date(), 'yyyyMMddHHmmss')}` : '');
link.download = `${name}.${type}`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}