feat: 工况库
This commit is contained in:
@@ -75,13 +75,35 @@ const download = (url: string, params = {}, filename = 'download') => {
|
||||
responseType: 'blob',
|
||||
})
|
||||
.then((res: any) => {
|
||||
// 从响应头获取文件名
|
||||
const contentDisposition = res.headers?.['content-disposition'] || '';
|
||||
const contentDisposition =
|
||||
res.headers?.['content-disposition'] ||
|
||||
res.headers?.['Content-Disposition'] ||
|
||||
'';
|
||||
let fileName = filename;
|
||||
const match = contentDisposition.match(/filename\*?=(?:UTF-8'')?["']?([^;"']+)/i);
|
||||
if (match && match[1]) {
|
||||
fileName = decodeURIComponent(match[1]);
|
||||
|
||||
if (contentDisposition) {
|
||||
const regex = /(filename\*?)\s*=\s*(?:UTF-8'')?["']?([^;"']+)["']?/ig;
|
||||
let match: RegExpExecArray | null = null;
|
||||
while ((match = regex.exec(contentDisposition)) !== null) {
|
||||
const key = match[1];
|
||||
const val = match[2];
|
||||
if (!val) continue;
|
||||
if (key.endsWith('*')) {
|
||||
if (val.toLowerCase() === 'utf-8') {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
fileName = decodeURIComponent(val);
|
||||
} catch {
|
||||
fileName = val;
|
||||
}
|
||||
} else {
|
||||
fileName = val.replace(/^["']|["']$/g, '');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const blob = new Blob([res.data]);
|
||||
const link = document.createElement('a');
|
||||
link.href = window.URL.createObjectURL(blob);
|
||||
|
||||
Reference in New Issue
Block a user