Skip to content

Commit c43e1fe

Browse files
committed
refactor: improve filename extraction logic in content disposition handling
1 parent 210e096 commit c43e1fe

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

ui/src/request/index.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,33 @@ export const exportExcel: (
249249
)
250250
}
251251

252-
function decodeFilenameBrowser(contentDisposition: string) {
253-
// 提取并解码 Base64 部分
254-
const base64Part = contentDisposition.match(/=\?utf-8\?b\?(.*?)\?=/i)?.[1]
255-
if (!base64Part) return null
252+
function extractFilename(contentDisposition: string) {
253+
if (!contentDisposition) return null;
256254

257-
// 使用 atob 解码 Base64
258-
const decoded = decodeURIComponent(escape(atob(base64Part)))
255+
// 处理 URL 编码的文件名
256+
const urlEncodedMatch = contentDisposition.match(/filename=([^;]*)/i) ||
257+
contentDisposition.match(/filename\*=UTF-8''([^;]*)/i);
258+
if (urlEncodedMatch && urlEncodedMatch[1]) {
259+
try {
260+
return decodeURIComponent(urlEncodedMatch[1].replace(/"/g, ''));
261+
} catch (e) {
262+
console.error("解码URL编码文件名失败:", e);
263+
}
264+
}
265+
266+
// 处理 Base64 编码的文件名
267+
const base64Part = contentDisposition.match(/=\?utf-8\?b\?(.*?)\?=/i)?.[1];
268+
if (base64Part) {
269+
try {
270+
const decoded = decodeURIComponent(escape(atob(base64Part)));
271+
const filenameMatch = decoded.match(/filename="(.*?)"/i);
272+
return filenameMatch ? filenameMatch[1] : null;
273+
} catch (e) {
274+
console.error("解码Base64文件名失败:", e);
275+
}
276+
}
259277

260-
// 提取文件名
261-
const filenameMatch = decoded.match(/filename="(.*?)"/i)
262-
return filenameMatch ? filenameMatch[1] : null
278+
return null;
263279
}
264280

265281
export const exportFile: (
@@ -296,9 +312,9 @@ export const exportFile: (
296312
// const contentType = headers['content-type'];
297313
const contentDisposition = headers['content-disposition']
298314
// console.log('Content-Type:', contentType);
299-
// console.log('Content-Disposition:', decodeFilenameBrowser(contentDisposition));
315+
// console.log('Content-Disposition:', contentDisposition);
300316
// 如果没有提供文件名,则使用默认名称
301-
fileName = decodeFilenameBrowser(contentDisposition) || fileName
317+
fileName = extractFilename(contentDisposition) || fileName
302318
return data // 必须返回数据
303319
},
304320
],

0 commit comments

Comments
 (0)