fix: add error handling and feedback for file downloads
Run Tests / Backend Tests (pull_request) Failing after 5m4s
Run Tests / Frontend Tests (pull_request) Failing after 1m5s
Run Tests / Test Summary (pull_request) Failing after 17s

This commit is contained in:
Gan, Jimmy
2026-04-13 10:25:40 +08:00
parent 1c3cbd187b
commit cdf8ab18fc
2 changed files with 55 additions and 16 deletions
+23 -14
View File
@@ -202,20 +202,29 @@ export async function download(path, filename) {
const headers = {};
if (token) headers["Authorization"] = `Bearer ${token}`;
const r = await fetch(`${BASE}/files/download?path=${encodeURIComponent(path)}`, {
method: "GET",
headers,
});
try {
const r = await fetch(`${BASE}/files/download?path=${encodeURIComponent(path)}`, {
method: "GET",
headers,
});
if (!r.ok) throw new Error(`HTTP ${r.status}`);
if (!r.ok) {
const errorText = await r.text();
console.error(`Download failed: ${r.status} - ${errorText}`);
throw new Error(`Download failed: ${r.status}`);
}
const blob = await r.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
const blob = await r.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch (e) {
console.error("Download error:", e);
throw e;
}
}