Files
syops/frontend/src/features/chat/shipping/shippingDownload.ts
T

30 lines
1.1 KiB
TypeScript

/**
* 出货聊天:发货 Excel 浏览器下载(触发 a[download])。
*/
import { workspaceShippingExportUrl } from '@/features/chat/api/shipping.api';
import { getAuthHeaders } from '@/shared/utils/auth';
export async function downloadWorkspaceShippingExcelFile(taskId: string): Promise<void> {
const url = workspaceShippingExportUrl(taskId);
const res = await fetch(url, { headers: getAuthHeaders() });
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error((body as { error?: string; message?: string })?.error || (body as { message?: string })?.message || res.statusText);
}
const disposition = res.headers.get('content-disposition');
const blob = await res.blob();
const objectUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = objectUrl;
const rawName = disposition?.match(/filename\*?=(?:UTF-8'')?["']?([^;"'\n]+)/)?.[1] || `${taskId}.xlsx`;
try {
a.download = decodeURIComponent(rawName);
} catch {
a.download = rawName;
}
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(objectUrl);
}