fix(report): 导出 PDF/Excel 用受控下载,文件名/扩展名正确
导出按钮(actionType:url)默认 window.open 一个 attachment 响应,部分浏览器丢失 Content-Disposition 文件名 → 下载成 UUID、无扩展名、打不开。新增 reportDownload:自定义 amis env.jumpTo 拦截报表导出 URL,显式 fetch 成 blob 并用 <a download="<CD文件名>"> 落盘。filenameFromContentDisposition 优先取 filename*(UTF-8 真实报表名)而非前面的 UUID 兜底。前端改动,无需重转重导,覆盖全部报表导出。6 单测 + 浏览器实测 办公用品采购申请单.pdf/.xls。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,7 @@ import { registerMessageAction } from '../amis/registerMessageAction';
|
|||||||
import { registerReplayAction } from '../amis/registerReplayAction';
|
import { registerReplayAction } from '../amis/registerReplayAction';
|
||||||
import { normalizeUserTransferSchemaTree, registerUserTransferPicker } from '../amis/registerUserTransferPicker';
|
import { normalizeUserTransferSchemaTree, registerUserTransferPicker } from '../amis/registerUserTransferPicker';
|
||||||
import { getProfile } from '../auth/token';
|
import { getProfile } from '../auth/token';
|
||||||
|
import { makeJumpTo } from './reportDownload';
|
||||||
|
|
||||||
interface AmisRendererProps {
|
interface AmisRendererProps {
|
||||||
schema: Record<string, unknown>;
|
schema: Record<string, unknown>;
|
||||||
@@ -141,6 +142,8 @@ export function AmisRenderer({ schema }: AmisRendererProps) {
|
|||||||
fetcher,
|
fetcher,
|
||||||
// 与后端 amis CRUD 约定对齐:status===0 为成功
|
// 与后端 amis CRUD 约定对齐:status===0 为成功
|
||||||
isCancel: (e: unknown) => (http as unknown as { isCancel?: (x: unknown) => boolean }).isCancel?.(e) ?? false,
|
isCancel: (e: unknown) => (http as unknown as { isCancel?: (x: unknown) => boolean }).isCancel?.(e) ?? false,
|
||||||
|
// 报表导出(actionType:url)走受控 blob 下载,按 Content-Disposition 命名,修「文件名 UUID/无扩展名」。
|
||||||
|
jumpTo: makeJumpTo(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
instanceRef.current = instance;
|
instanceRef.current = instance;
|
||||||
|
|||||||
41
src/components/reportDownload.test.ts
Normal file
41
src/components/reportDownload.test.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
|
||||||
|
import { filenameFromContentDisposition, isReportDownloadUrl } from './reportDownload';
|
||||||
|
|
||||||
|
describe('filenameFromContentDisposition', () => {
|
||||||
|
it('优先取 filename* 的 UTF-8 中文报表名(而非前面的 UUID 兜底)', () => {
|
||||||
|
// 后端真实响应头形态:ASCII filename 是 UUID 兜底,filename* 才是真实报表名
|
||||||
|
const cd =
|
||||||
|
'attachment; filename="0a47a300-f544-42fd-b523-f4ac9672e27b.xls"; '
|
||||||
|
+ "filename*=UTF-8''%E5%8A%9E%E5%85%AC%E7%94%A8%E5%93%81%E9%87%87%E8%B4%AD%E7%94%B3%E8%AF%B7%E5%8D%95.xls";
|
||||||
|
expect(filenameFromContentDisposition(cd)).toBe('办公用品采购申请单.xls');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('无 filename* 时回退普通 filename', () => {
|
||||||
|
expect(filenameFromContentDisposition('attachment; filename="report.pdf"')).toBe('report.pdf');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filename* 中的 + 还原为空格', () => {
|
||||||
|
expect(filenameFromContentDisposition("attachment; filename*=UTF-8''a+b.xls")).toBe('a b.xls');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('空/缺失返回空串', () => {
|
||||||
|
expect(filenameFromContentDisposition(undefined)).toBe('');
|
||||||
|
expect(filenameFromContentDisposition(null)).toBe('');
|
||||||
|
expect(filenameFromContentDisposition('')).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('isReportDownloadUrl', () => {
|
||||||
|
it('匹配 report render 的 pdf/xls 导出 URL', () => {
|
||||||
|
expect(isReportDownloadUrl('/api/report/render/abc?format=pdf')).toBe(true);
|
||||||
|
expect(isReportDownloadUrl('/api/report/render/abc?format=xls&OA_X_ID=1')).toBe(true);
|
||||||
|
expect(isReportDownloadUrl('/api/report/render/abc?format=xlsx')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('不匹配 html(iframe 查看) 或非报表 URL', () => {
|
||||||
|
expect(isReportDownloadUrl('/api/report/render/abc?format=html')).toBe(false);
|
||||||
|
expect(isReportDownloadUrl('/api/report/frame/abc')).toBe(false);
|
||||||
|
expect(isReportDownloadUrl('/page/abc')).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
90
src/components/reportDownload.ts
Normal file
90
src/components/reportDownload.ts
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
// ============================================================================
|
||||||
|
// 报表导出下载 —— 受控 blob 下载,按 Content-Disposition 正确命名
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// 背景:报表导出按钮是 amis actionType:url(默认 window.open)。部分浏览器在
|
||||||
|
// window.open 一个 attachment 响应时会丢失 Content-Disposition 文件名,退化成
|
||||||
|
// blob/路径命名(UUID、无扩展名,双击打不开)。这里把报表导出 URL 拦下来,显式
|
||||||
|
// fetch 成 blob 并用 <a download="<CD文件名>"> 落盘,文件名/扩展名在所有浏览器都正确。
|
||||||
|
// ============================================================================
|
||||||
|
import { http } from '../api/http';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 Content-Disposition 文件名。**优先 {@code filename*=UTF-8''…}**(RFC 5987,含中文报表名,
|
||||||
|
* URL 解码);否则回退普通 {@code filename="…"}(常是 UUID/ASCII 兜底)。
|
||||||
|
* 注意:CD 形如 {@code attachment; filename="UUID.xls"; filename*=UTF-8''办公用品采购申请单.xls},
|
||||||
|
* 必须先取 filename*,否则会取到前面的 UUID。
|
||||||
|
*/
|
||||||
|
export function filenameFromContentDisposition(cd: string | undefined | null): string {
|
||||||
|
if (!cd) return '';
|
||||||
|
// 1) 优先 filename*(UTF-8 编码的真实报表名)
|
||||||
|
const star = /filename\*=(?:UTF-8'')?([^;\n]*)/i.exec(cd);
|
||||||
|
if (star && star[1]) {
|
||||||
|
let f = star[1].trim().replace(/['"]/g, '');
|
||||||
|
try {
|
||||||
|
f = decodeURIComponent(f);
|
||||||
|
} catch {
|
||||||
|
/* 解码失败保留原值 */
|
||||||
|
}
|
||||||
|
return f.replace(/\+/g, ' ');
|
||||||
|
}
|
||||||
|
// 2) 回退普通 filename="…"(不匹配 filename*=,因其含 '*')
|
||||||
|
const plain = /filename=("?)([^";\n]*)\1/i.exec(cd);
|
||||||
|
let f = plain && plain[2] ? plain[2].trim() : '';
|
||||||
|
if (f && f.replace(/[^%]/g, '').length > 2) {
|
||||||
|
try {
|
||||||
|
f = decodeURIComponent(f);
|
||||||
|
} catch {
|
||||||
|
/* 保留原值 */
|
||||||
|
}
|
||||||
|
f = f.replace(/\+/g, ' ');
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 报表导出下载 URL(format=pdf/xls/…)——应作为带正确文件名的附件落盘,而非 window.open。 */
|
||||||
|
export function isReportDownloadUrl(to: string): boolean {
|
||||||
|
return /\/api\/report\/render\//.test(to) && /[?&]format=(pdf|xls|xlsx|excel)(\b|&|$)/i.test(to);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以 blob 拉取报表导出并按 Content-Disposition 文件名落盘({@code <a download>})。
|
||||||
|
* 显式取 CD 文件名并用 download 属性强制命名,规避 window.open 在部分浏览器丢失文件名的问题。
|
||||||
|
*/
|
||||||
|
export async function downloadReportFile(to: string): Promise<void> {
|
||||||
|
const resp = (await http.get(to, { responseType: 'blob' as never })) as {
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
data?: unknown;
|
||||||
|
};
|
||||||
|
const headers = resp.headers ?? {};
|
||||||
|
const filename = filenameFromContentDisposition(headers['content-disposition']) || 'report';
|
||||||
|
const blob = resp.data instanceof Blob ? resp.data : new Blob([resp.data as BlobPart]);
|
||||||
|
const objectUrl = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = objectUrl;
|
||||||
|
a.download = filename;
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
a.remove();
|
||||||
|
setTimeout(() => URL.revokeObjectURL(objectUrl), 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 自定义 jumpTo:报表导出 URL 走受控 blob 下载(文件名正确),其余沿用 amis 默认跳转语义。 */
|
||||||
|
export function makeJumpTo() {
|
||||||
|
return (to: string, action?: { actionType?: string; blank?: boolean }) => {
|
||||||
|
if (to && isReportDownloadUrl(to)) {
|
||||||
|
void downloadReportFile(to);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (to === 'goBack') {
|
||||||
|
window.history.back();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (action && action.actionType === 'url') {
|
||||||
|
if (action.blank === false) window.location.href = to;
|
||||||
|
else window.open(to);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (/^https?:\/\//.test(to)) window.location.replace(to);
|
||||||
|
else window.location.href = to;
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user