优化查看源文件报objact
This commit is contained in:
1 parent
854bc8e4db
commit
07a1494a1e
2 files changed
+69
-22
No files matched your search
@@ -129,6 +129,42 @@ const clearOfficeObjectUrl = () => {
|
||||
officeKind.value = null;
|
||||
};
|
||||
|
||||
const extractErrorMessage = (payload: any, fallback: string) => {
|
||||
if (!payload) return fallback;
|
||||
if (typeof payload === 'string' && payload.trim()) return payload.trim();
|
||||
if (typeof payload?.message === 'string' && payload.message.trim()) return payload.message.trim();
|
||||
if (typeof payload?.error === 'string' && payload.error.trim()) return payload.error.trim();
|
||||
if (typeof payload?.error?.message === 'string' && payload.error.message.trim()) return payload.error.message.trim();
|
||||
if (typeof payload?.detail === 'string' && payload.detail.trim()) return payload.detail.trim();
|
||||
return fallback;
|
||||
};
|
||||
|
||||
const readErrorMessage = async (res: Response) => {
|
||||
const fallback = `HTTP ${res.status}`;
|
||||
const contentType = (res.headers.get('content-type') || '').toLowerCase();
|
||||
if (contentType.includes('application/json')) {
|
||||
const payload = await res.json().catch(() => null);
|
||||
return extractErrorMessage(payload, fallback);
|
||||
}
|
||||
const text = await res.text().catch(() => '');
|
||||
return extractErrorMessage(text, fallback);
|
||||
};
|
||||
|
||||
const extractDownloadFilename = (res: Response) => {
|
||||
const disposition = res.headers.get('content-disposition') || '';
|
||||
const utf8Match = disposition.match(/filename\*=UTF-8''([^;]+)/i);
|
||||
if (utf8Match?.[1]) {
|
||||
try {
|
||||
return decodeURIComponent(utf8Match[1]);
|
||||
} catch {
|
||||
return utf8Match[1];
|
||||
}
|
||||
}
|
||||
const asciiMatch = disposition.match(/filename="([^"]+)"/i);
|
||||
if (asciiMatch?.[1]) return asciiMatch[1];
|
||||
return '';
|
||||
};
|
||||
|
||||
const applyExcelPreview = (buffer: ArrayBuffer) => {
|
||||
const workbook = XLSX.read(buffer, { type: 'array' });
|
||||
if (!workbook.SheetNames.length) {
|
||||
@@ -182,8 +218,7 @@ const loadSheetPreview = async () => {
|
||||
headers: props.authHeaders || {},
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data?.error || `HTTP ${res.status}`);
|
||||
throw new Error(await readErrorMessage(res));
|
||||
}
|
||||
|
||||
excelPreview.value = await res.json();
|
||||
@@ -202,8 +237,7 @@ const loadOfficeContent = async () => {
|
||||
headers: props.authHeaders || {},
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err?.error || `HTTP ${res.status}`);
|
||||
throw new Error(await readErrorMessage(res));
|
||||
}
|
||||
|
||||
const buffer = await res.arrayBuffer();
|
||||
@@ -238,8 +272,7 @@ const loadPdfContent = async (): Promise<'iframe' | 'office' | 'sheet'> => {
|
||||
headers: props.authHeaders || {},
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err?.error || `HTTP ${res.status}`);
|
||||
throw new Error(await readErrorMessage(res));
|
||||
}
|
||||
|
||||
const blob = await res.blob();
|
||||
@@ -290,8 +323,7 @@ const loadBinaryContent = async () => {
|
||||
headers: props.authHeaders || {},
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err?.error || `HTTP ${res.status}`);
|
||||
throw new Error(await readErrorMessage(res));
|
||||
}
|
||||
|
||||
const blob = await res.blob();
|
||||
@@ -314,15 +346,14 @@ const downloadFile = async () => {
|
||||
headers: props.authHeaders || {},
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err?.error || `HTTP ${res.status}`);
|
||||
throw new Error(await readErrorMessage(res));
|
||||
}
|
||||
|
||||
const blob = await res.blob();
|
||||
const downloadUrl = URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = downloadUrl;
|
||||
link.download = props.file.name || 'download';
|
||||
link.download = extractDownloadFilename(res) || props.file.name || 'download';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
|
||||
Reference in new issue
Block a user