import { message } from 'antd'; /** * 文件下载函数 * * @category Utils * @param param0 : 包括 data: 文件内容;fileName: 文件名;type: 文件类型 */ export const downloadFile = ({ data, fileName, type, }: { data: any; fileName: string; type: string; }) => { const blob = new Blob([data], { type, }); const reader = new FileReader(); reader.readAsText(blob, 'utf-8'); reader.onload = () => { try { const res = JSON.parse((reader as any).result); message.error(res.message); } catch (e) { const downloadElement = document.createElement('a'); downloadElement.download = fileName; downloadElement.href = window.URL.createObjectURL(blob); downloadElement.click(); document.body.appendChild(downloadElement); document.body.removeChild(downloadElement); window.URL.revokeObjectURL(downloadElement.href); } }; };