BlueGit

timfa filemanager

fix: send auth header for sidebar folder zip download

A bare <a> click cannot attach Authorization headers, causing 401.
Now uses fetch() with Bearer token to get the zip as a blob, then
triggers download via a temporary blob URL.

timfa Tim Falken · pushed by timfa

Authored 2026-05-06 · 6 months ago

Hash
09b0cba131bd8b08f78bebecb1b8c53c6e2d5a87
Parent
9509bab0
1 file changed +25 −4
public/app.js +25 −4
@@ -1319,11 +1319,32 @@ async function openSidebar (item)
1319 1319 btnZip.textContent = 'Download als zip';
1320 1320 btnZip.addEventListener('click', () =>
1321 1321 {
1322 btnZip.disabled = true;
1323 btnZip.textContent = 'Bezig…';
1322 1324 const url = `/api/files/download-zip?dir=${encodeURIComponent(item.path)}`;
1323 const a = document.createElement('a');
1324 a.href = url;
1325 a.download = item.name + '.zip';
1326 a.click();
1325 const headers = { 'Content-Type': 'application/json' };
1326 if (STATE.token) headers['Authorization'] = 'Bearer ' + STATE.token;
1327 fetch(url, { headers })
1328 .then(res =>
1329 {
1330 if (!res.ok) throw new Error('Download mislukt');
1331 return res.blob();
1332 })
1333 .then(blob =>
1334 {
1335 const blobUrl = URL.createObjectURL(blob);
1336 const a = document.createElement('a');
1337 a.href = blobUrl;
1338 a.download = item.name + '.zip';
1339 a.click();
1340 setTimeout(() => URL.revokeObjectURL(blobUrl), 10000);
1341 })
1342 .catch((err) => toast(err.message || 'Download mislukt', 'err'))
1343 .finally(() =>
1344 {
1345 btnZip.disabled = false;
1346 btnZip.textContent = 'Download als zip';
1347 });
1327 1348 });
1328 1349 actions.appendChild(btnZip);
1329 1350
@@ -1319,11 +1319,32 @@ async function openSidebar (item)
1319 btnZip.textContent = 'Download als zip';
1319 btnZip.textContent = 'Download als zip';
1320 btnZip.addEventListener('click', () =>
1320 btnZip.addEventListener('click', () =>
1321 {
1321 {
1322 btnZip.disabled = true;
1323 btnZip.textContent = 'Bezig…';
1322 const url = `/api/files/download-zip?dir=${encodeURIComponent(item.path)}`;
1324 const url = `/api/files/download-zip?dir=${encodeURIComponent(item.path)}`;
1323 const a = document.createElement('a');
1325 const headers = { 'Content-Type': 'application/json' };
1324 a.href = url;
1326 if (STATE.token) headers['Authorization'] = 'Bearer ' + STATE.token;
1325 a.download = item.name + '.zip';
1327 fetch(url, { headers })
1326 a.click();
1328 .then(res =>
1329 {
1330 if (!res.ok) throw new Error('Download mislukt');
1331 return res.blob();
1332 })
1333 .then(blob =>
1334 {
1335 const blobUrl = URL.createObjectURL(blob);
1336 const a = document.createElement('a');
1337 a.href = blobUrl;
1338 a.download = item.name + '.zip';
1339 a.click();
1340 setTimeout(() => URL.revokeObjectURL(blobUrl), 10000);
1341 })
1342 .catch((err) => toast(err.message || 'Download mislukt', 'err'))
1343 .finally(() =>
1344 {
1345 btnZip.disabled = false;
1346 btnZip.textContent = 'Download als zip';
1347 });
1327 });
1348 });
1328 actions.appendChild(btnZip);
1349 actions.appendChild(btnZip);
1329
1350