// * desc: 下载导出文件 // * @param blob :返回数据的blob对象或链接 // * @param fileName :下载后文件名标记 // * @param fileType :文件类 word(docx) excel(xlsx) ppt等 function downloadExportFile( blob: Blob | MediaSource, fileName: string, fileType: string, ) { const downloadElement = document.createElement('a'); let href: string | Blob | MediaSource = blob; if (typeof blob === 'string') { downloadElement.target = '_blank'; } else { href = window.URL.createObjectURL(blob); // 创建下载的链接 } downloadElement.href = href as any; downloadElement.download = fileName + '.' + fileType; // 下载后文件名 document.body.appendChild(downloadElement); downloadElement.click(); // 触发点击下载 document.body.removeChild(downloadElement); // 下载完成移除元素 if (typeof blob !== 'string') { window.URL.revokeObjectURL(href as any); // 释放掉blob对象 } } // * desc: base64转文件并下载 // * @param base64 {String} : base64数据 // * @param fileType {String} : 要导出的文件类型png,pdf,doc,mp3等 // * @param fileName {String} : 文件名 export function downloadFile(base64: string, fileName: any, fileType: string) { const typeHeader = 'data:application/' + fileType + ';base64,'; // 定义base64 头部文件类型 const converedBase64 = typeHeader + base64; // 拼接最终的base64 const blob = this.base64ToBlob(converedBase64, fileType); // 转成blob对象 downloadExportFile(blob, fileName, fileType); // 下载文件 }