fix: 下载文件
This commit is contained in:
@@ -84,25 +84,52 @@ const download = (url: string, params = {}, filename = 'download') => {
|
||||
let fileName = filename;
|
||||
|
||||
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;
|
||||
}
|
||||
const parts = contentDisposition.split(';').map((p: string) => p.trim()).filter(Boolean);
|
||||
const paramsMap: Record<string, string> = {};
|
||||
parts.forEach((part: string) => {
|
||||
const idx = part.indexOf('=');
|
||||
if (idx === -1) return;
|
||||
const key = part.slice(0, idx).trim().toLowerCase();
|
||||
let val = part.slice(idx + 1).trim();
|
||||
if (val.startsWith('"') && val.endsWith('"')) {
|
||||
val = val.slice(1, -1);
|
||||
}
|
||||
paramsMap[key] = val;
|
||||
});
|
||||
|
||||
if (paramsMap['filename*']) {
|
||||
const val = paramsMap['filename*'];
|
||||
const rfcMatch = val.match(/^([^']*)''(.*)$/);
|
||||
if (rfcMatch) {
|
||||
const encoded = rfcMatch[2];
|
||||
try {
|
||||
fileName = decodeURIComponent(val);
|
||||
fileName = decodeURIComponent(encoded);
|
||||
} catch {
|
||||
fileName = val;
|
||||
fileName = encoded;
|
||||
}
|
||||
} else {
|
||||
fileName = val.replace(/^["']|["']$/g, '');
|
||||
if (/^utf-?8$/i.test(val) && paramsMap['filename']) {
|
||||
const candidate = paramsMap['filename'];
|
||||
try {
|
||||
fileName = candidate.includes('%') ? decodeURIComponent(candidate) : candidate;
|
||||
} catch {
|
||||
fileName = candidate;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
fileName = val.includes('%') ? decodeURIComponent(val) : val;
|
||||
} catch {
|
||||
fileName = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (paramsMap['filename']) {
|
||||
const candidate = paramsMap['filename'];
|
||||
try {
|
||||
fileName = candidate.includes('%') ? decodeURIComponent(candidate) : candidate;
|
||||
} catch {
|
||||
fileName = candidate;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user