From 66bb97fcdffb0fda8ca2129df7bc8fcd6f521d8f Mon Sep 17 00:00:00 2001 From: kicer Date: Fri, 4 Sep 2026 14:27:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=B9=E9=87=8F=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=89=8B=E6=9C=BA=E7=AB=AF=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E9=94=99=E4=B9=B1/=E5=A5=97=E5=A3=B3=20App=20=E6=97=A0?= =?UTF-8?q?=E5=8F=8D=E5=BA=94=EF=BC=9A=E6=94=BE=E5=BC=83=20blob=20?= =?UTF-8?q?=E6=96=B9=E6=A1=88=EF=BC=8C=E5=85=88=E6=89=B9=E9=87=8F=E6=8B=BF?= =?UTF-8?q?=20token=20=E5=86=8D=E7=94=A8=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E7=9B=B4=E9=93=BE=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - blob: URL 手机浏览器不认下载名(变编号/.bin),WebView 套壳不处理 blob 下载请求 - 回到与单文件下载一致的直链路径:文件名跟服务端 Content-Disposition / download-name 兜底 - 所有 download-token 先全部拿到再逐个 触发,规避首个导航打断后续 fetch(原第 2 个失败的根因) - 逐个触发间隔 350ms,避免移动端拦截连续多次下载 --- app/server/public/index.html | 41 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/app/server/public/index.html b/app/server/public/index.html index 950ab73..3a7b27c 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -1468,34 +1468,37 @@ else downloadFile(path); }); - // 批量下载:逐个 fetch 同源 URL 拿到 blob 后,用 触发下载。 - // 不走 window.location.href,避免顶层导航被浏览器串行/去重、丢 cookie。 + // 批量下载:先把所有 download-token 拿完,再用服务器直链逐个触发下载。 + // 两个历史坑: + // 1) 边拿 token 边 location.href:首个下载导航会打断后续 fetch(第 2 个报失败); + // → 先批量拿完 token 再触发。 + // 2) fetch+blob 方案:手机浏览器不认 blob 文件名(变编号/.bin), + // 套壳 WebView 不处理 blob: 下载请求(点了没反应); + // → 回到服务器原始直链(文件名跟 Content-Disposition),与单文件下载同一条链路。 async function bulkDownload() { if (!state.sessionId || fileSelected.size === 0) return; const names = Array.from(fileSelected); + // 第一阶段:全部 token 先拿到手(此时还没发生任何导航,fetch 不会被打断) + const items = []; 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) 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); + items.push({ url: r.url, name: String(name).split('/').pop() || name }); } catch (e) { toast('下载失败:' + name + ' ' + e.message); } } + // 第二阶段:隐藏 + 服务器直链逐个触发;download 属性作手机端文件名兜底, + // 小间隔避免移动端拦截连续多次下载 + for (const it of items) { + const a = document.createElement('a'); + a.href = it.url; + a.download = it.name; + a.style.display = 'none'; + document.body.appendChild(a); + a.click(); + setTimeout(() => { try { a.remove(); } catch (_) {} }, 1000); + await new Promise(res => setTimeout(res, 350)); + } } async function bulkDelete() { if (!state.sessionId || fileSelected.size === 0) return;