|
|
|
@@ -1,19 +1,21 @@ |
|
|
|
import { request } from '@umijs/max'; |
|
|
|
|
|
|
|
const mimeMap = { |
|
|
|
/** MimeType */ |
|
|
|
export const mimeMap = { |
|
|
|
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
|
|
zip: 'application/zip', |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* 解析blob响应内容并下载 |
|
|
|
* @param {*} res blob响应内容 |
|
|
|
* @param {String} mimeType MIME类型 |
|
|
|
* @param res - blob响应内容 |
|
|
|
* @param mimeType - MIME类型 |
|
|
|
*/ |
|
|
|
export function resolveBlob(res: any, mimeType: string) { |
|
|
|
const aLink = document.createElement('a'); |
|
|
|
const blob = new Blob([res.data], { type: mimeType }); |
|
|
|
// //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名; |
|
|
|
// 从response的headers中获取filename, |
|
|
|
// 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名; |
|
|
|
const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*'); |
|
|
|
// console.log(res); |
|
|
|
const contentDisposition = decodeURI(res.headers['content-disposition']); |
|
|
|
@@ -29,6 +31,11 @@ export function resolveBlob(res: any, mimeType: string) { |
|
|
|
document.body.removeChild(aLink); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 下载 Zip 文件 |
|
|
|
* @param url - url 地址 |
|
|
|
* @param options - 请求参数 |
|
|
|
*/ |
|
|
|
export function downLoadZip(url: string, params?: any) { |
|
|
|
request(url, { |
|
|
|
method: 'GET', |
|
|
|
@@ -40,24 +47,30 @@ export function downLoadZip(url: string, params?: any) { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
export async function downLoadXlsx(url: string, params: any, fileName: string) { |
|
|
|
/** |
|
|
|
* 下载 XLSX 文件 |
|
|
|
* @param url - url 地址 |
|
|
|
* @param method - 请求方式 |
|
|
|
* @param options - 请求选项 |
|
|
|
*/ |
|
|
|
export async function downloadXlsx( |
|
|
|
url: string, |
|
|
|
method: string = 'GET', |
|
|
|
options?: Record<string, any>, |
|
|
|
) { |
|
|
|
return request(url, { |
|
|
|
...params, |
|
|
|
method: 'POST', |
|
|
|
method: method, |
|
|
|
...options, |
|
|
|
responseType: 'blob', |
|
|
|
}).then((data) => { |
|
|
|
const aLink = document.createElement('a'); |
|
|
|
const blob = data as any; // new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); |
|
|
|
aLink.style.display = 'none'; |
|
|
|
aLink.href = URL.createObjectURL(blob); |
|
|
|
aLink.setAttribute('download', fileName); // 设置下载文件名称 |
|
|
|
document.body.appendChild(aLink); |
|
|
|
aLink.click(); |
|
|
|
URL.revokeObjectURL(aLink.href); // 清除引用 |
|
|
|
document.body.removeChild(aLink); |
|
|
|
getResponse: true, |
|
|
|
}).then((res) => { |
|
|
|
resolveBlob(res, mimeMap.xlsx); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @deprecated 无效 |
|
|
|
*/ |
|
|
|
export function download(fileName: string) { |
|
|
|
window.location.href = `/api/common/download?fileName=${encodeURI(fileName)}&delete=${true}`; |
|
|
|
} |