feat: name export file with meaningful title instead of id (#379)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { getFilenameFromContentDisposition } from '@/utils/request';
|
||||
import { logError } from '@/utils/errorHandler';
|
||||
import Config from '@/config/config';
|
||||
const apiServer = Config.apiServer;
|
||||
@@ -221,11 +222,14 @@ async function exportCases(jwt: string, folderId: number, type: string) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
|
||||
const disposition = response.headers.get('content-disposition');
|
||||
const filename = getFilenameFromContentDisposition(disposition) ?? `cases.${type}`;
|
||||
|
||||
const blob = await response.blob();
|
||||
const objectUrl = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = objectUrl;
|
||||
a.download = `folder_${folderId}.${type}`;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
|
||||
17
frontend/utils/request.ts
Normal file
17
frontend/utils/request.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export function getFilenameFromContentDisposition(disposition: string | null): string | null {
|
||||
if (!disposition) return null;
|
||||
|
||||
const filenameStar = disposition.match(/filename\*\s*=\s*UTF-8''([^;]+)/i);
|
||||
if (filenameStar?.[1]) {
|
||||
try {
|
||||
return decodeURIComponent(filenameStar[1]);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
const filename = disposition.match(/filename\s*=\s*"([^"]+)"/i) || disposition.match(/filename\s*=\s*([^;]+)/i);
|
||||
if (filename?.[1]) return filename[1].trim();
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user