Skip to content

Commit d8132fa

Browse files
committed
feat: add filename decoding from Content-Disposition header in exportFile function
1 parent bed9c50 commit d8132fa

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

ui/src/request/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,19 @@ export const exportExcel: (
247247
)
248248
}
249249

250+
function decodeFilenameBrowser(contentDisposition: string) {
251+
// 提取并解码 Base64 部分
252+
const base64Part = contentDisposition.match(/=\?utf-8\?b\?(.*?)\?=/i)?.[1];
253+
if (!base64Part) return null;
254+
255+
// 使用 atob 解码 Base64
256+
const decoded = decodeURIComponent(escape(atob(base64Part)));
257+
258+
// 提取文件名
259+
const filenameMatch = decoded.match(/filename="(.*?)"/i);
260+
return filenameMatch ? filenameMatch[1] : null;
261+
}
262+
250263
export const exportFile: (
251264
fileName: string,
252265
url: string,
@@ -258,7 +271,22 @@ export const exportFile: (
258271
params: any,
259272
loading?: NProgress | Ref<boolean>,
260273
) => {
261-
return promise(request({ url: url, method: 'get', params, responseType: 'blob' }), loading).then(
274+
return promise(request({
275+
url: url,
276+
method: 'get',
277+
params,
278+
responseType: 'blob',
279+
transformResponse: [function (data, headers) {
280+
// 在这里可以访问 headers
281+
// const contentType = headers['content-type'];
282+
const contentDisposition = headers['content-disposition'];
283+
// console.log('Content-Type:', contentType);
284+
// console.log('Content-Disposition:', decodeFilenameBrowser(contentDisposition));
285+
// 如果没有提供文件名,则使用默认名称
286+
fileName = decodeFilenameBrowser(contentDisposition) || fileName;
287+
return data; // 必须返回数据
288+
}]
289+
}), loading).then(
262290
(res: any) => {
263291
if (res) {
264292
const blob = new Blob([res], {

0 commit comments

Comments
 (0)