From cedd236dd8c2fbf7042d3ddbfb98d21dd526d3fa Mon Sep 17 00:00:00 2001 From: kicer Date: Fri, 4 Sep 2026 09:50:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=89=B9=E9=87=8F=E4=B8=8B=E8=BD=BD=EF=BC=9A=E6=94=B9?= =?UTF-8?q?=E7=94=A8=20fetch+blob=20=E8=A7=A6=E5=8F=91=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=20window.location.href=20=E5=9C=A8?= =?UTF-8?q?=E5=A4=9A=E6=96=87=E4=BB=B6=E5=9C=BA=E6=99=AF=E8=A2=AB=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E5=8E=BB=E9=87=8D/=E4=B8=A2=20cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/public/index.html | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/server/public/index.html b/app/server/public/index.html index b134d60..bde0ebb 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -1423,14 +1423,32 @@ else downloadFile(path); }); - // 批量下载:逐个拉取 download-token 后跳转,复用单文件下载路径(多个文件浏览器会连续触发下载) + // 批量下载:逐个 fetch 同源 URL 拿到 blob 后,用 触发下载。 + // 不走 window.location.href,避免顶层导航被浏览器串行/去重、丢 cookie。 async function bulkDownload() { if (!state.sessionId || fileSelected.size === 0) return; const names = Array.from(fileSelected); for (const name of names) { try { + // 后端返回的 download-token 里含一个带一次性 token 的直链(同源) const r = await req('GET', '/api/sessions/' + state.sessionId + '/files/download-token?path=' + encodeURIComponent(name)); - if (r && r.url) window.location.href = r.url; + if (!r || !r.url) throw new Error('未拿到下载链接'); + // 同源 fetch:明确带 credentials,兼容 Safari 严格 cookie 策略与反代部署 + const resp = await fetch(r.url, { credentials: 'same-origin' }); + if (!resp.ok) { let m = 'HTTP ' + resp.status; try { m = (await resp.json()).error || m; } catch (_) {} throw new Error(m); } + const blob = await resp.blob(); + const blobUrl = URL.createObjectURL(blob); + // 取文件名(剥路径)作为下载名 + const baseName = String(name).split('/').pop() || name; + const a = document.createElement('a'); + a.href = blobUrl; + a.download = baseName; + a.rel = 'noopener'; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + // 释放 blob URL;稍微延迟避免 Safari 取消下载 + setTimeout(() => { try { URL.revokeObjectURL(blobUrl); } catch (_) {} }, 4000); } catch (e) { toast('下载失败:' + name + ' ' + e.message); } } }