1、封装get请求并暴露
// 通用get请求
function httpGet(url: string, params?: any, headers?: any, attrs?: any): any;
2、封装函数
// 下载文件
export function getDownload(fileName, filePath) {
httpGet(
'/upload/getFileByFileNameAndFilePath',
{ fileName: fileName, filePath: filePath },
{},
{ responseType: 'arraybuffer' }
).then((res: any) => {
if (res) {
const blob = new Blob([res], { type: 'application/octet-stream;charset=utf-8' })
// 组装a标签
const elink = document.createElement('a')
// 设置下载文件名
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
document.body.removeChild(elink)
URL.revokeObjectURL(elink.href)
}
})
}