fix: add error handling and feedback for file downloads
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user