//导出
const exportExcel = () => {
const ids = getRowSelection().selectedRowKeys;
var form = getForm().getFieldsValue();
if (dates.value && dates.value.length) {
let date = JSON.parse(JSON.stringify(dates.value));
form.beginTime = date[0];
form.endTime = date[1];
}
if (ids.length) form.ids = ids.join(',');
createConfirm({
iconType: 'warning',
title: () => h('span', '确认'),
content: () => h('span', '是否导出指标数据?'),
onOk: async () => {
const data = await exportIndex({
...form,
});
downloadByData(data, '告警数据.xlsx');
},
});
};
import { defHttp } from '/@/utils/http/axios';
export const exportIndex = (params?: object) =>
defHttp.get(
{
url: Api.exportIndex,
params,
responseType: 'blob',
},
{ errorMessageMode: 'none', isTransformResponse: false },
);
/**
* Download according to the background interface file stream
* @param {*} data
* @param {*} filename
* @param {*} mime
* @param {*} bom
*/
export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {
const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
const blobURL = window.URL.createObjectURL(blob);
const tempLink = document.createElement('a');
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute('download', filename);
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank');
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
responseType: 'blob'
表示期望服务器返回的数据类型是二进制大对象(Blob)。这对于下载文件非常重要,因为浏览器可以直接处理Blob来触发文件下载,而不是尝试将其解析为文本。{ isTransformResponse: false }
表示禁用响应数据的自动转换处理。默认情况下,某些HTTP库会尝试将响应体自动转换为JSON或其他格式,这里关闭该功能是因为当responseType
设为blob
时,不需要也不应该进行数据转换,直接处理原始的Blob数据即可。