修复批量下载手机端文件名错乱/套壳 App 无反应:放弃 blob 方案,先批量拿 token 再用服务器直链触发
- blob: URL 手机浏览器不认下载名(变编号/.bin),WebView 套壳不处理 blob 下载请求 - 回到与单文件下载一致的直链路径:文件名跟服务端 Content-Disposition / download-name 兜底 - 所有 download-token 先全部拿到再逐个 <a> 触发,规避首个导航打断后续 fetch(原第 2 个失败的根因) - 逐个触发间隔 350ms,避免移动端拦截连续多次下载
This commit is contained in:
@@ -1468,33 +1468,36 @@
|
|||||||
else downloadFile(path);
|
else downloadFile(path);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 批量下载:逐个 fetch 同源 URL 拿到 blob 后,用 <a download> 触发下载。
|
// 批量下载:先把所有 download-token 拿完,再用服务器直链逐个触发下载。
|
||||||
// 不走 window.location.href,避免顶层导航被浏览器串行/去重、丢 cookie。
|
// 两个历史坑:
|
||||||
|
// 1) 边拿 token 边 location.href:首个下载导航会打断后续 fetch(第 2 个报失败);
|
||||||
|
// → 先批量拿完 token 再触发。
|
||||||
|
// 2) fetch+blob 方案:手机浏览器不认 blob 文件名(变编号/.bin),
|
||||||
|
// 套壳 WebView 不处理 blob: 下载请求(点了没反应);
|
||||||
|
// → 回到服务器原始直链(文件名跟 Content-Disposition),与单文件下载同一条链路。
|
||||||
async function bulkDownload() {
|
async function bulkDownload() {
|
||||||
if (!state.sessionId || fileSelected.size === 0) return;
|
if (!state.sessionId || fileSelected.size === 0) return;
|
||||||
const names = Array.from(fileSelected);
|
const names = Array.from(fileSelected);
|
||||||
|
// 第一阶段:全部 token 先拿到手(此时还没发生任何导航,fetch 不会被打断)
|
||||||
|
const items = [];
|
||||||
for (const name of names) {
|
for (const name of names) {
|
||||||
try {
|
try {
|
||||||
// 后端返回的 download-token 里含一个带一次性 token 的直链(同源)
|
|
||||||
const r = await req('GET', '/api/sessions/' + state.sessionId + '/files/download-token?path=' + encodeURIComponent(name));
|
const r = await req('GET', '/api/sessions/' + state.sessionId + '/files/download-token?path=' + encodeURIComponent(name));
|
||||||
if (!r || !r.url) throw new Error('未拿到下载链接');
|
if (!r || !r.url) throw new Error('未拿到下载链接');
|
||||||
// 同源 fetch:明确带 credentials,兼容 Safari 严格 cookie 策略与反代部署
|
items.push({ url: r.url, name: String(name).split('/').pop() || name });
|
||||||
const resp = await fetch(r.url, { credentials: 'same-origin' });
|
} catch (e) { toast('下载失败:' + name + ' ' + e.message); }
|
||||||
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();
|
// 第二阶段:隐藏 <a> + 服务器直链逐个触发;download 属性作手机端文件名兜底,
|
||||||
const blobUrl = URL.createObjectURL(blob);
|
// 小间隔避免移动端拦截连续多次下载
|
||||||
// 取文件名(剥路径)作为下载名
|
for (const it of items) {
|
||||||
const baseName = String(name).split('/').pop() || name;
|
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = blobUrl;
|
a.href = it.url;
|
||||||
a.download = baseName;
|
a.download = it.name;
|
||||||
a.rel = 'noopener';
|
a.style.display = 'none';
|
||||||
document.body.appendChild(a);
|
document.body.appendChild(a);
|
||||||
a.click();
|
a.click();
|
||||||
document.body.removeChild(a);
|
setTimeout(() => { try { a.remove(); } catch (_) {} }, 1000);
|
||||||
// 释放 blob URL;稍微延迟避免 Safari 取消下载
|
await new Promise(res => setTimeout(res, 350));
|
||||||
setTimeout(() => { try { URL.revokeObjectURL(blobUrl); } catch (_) {} }, 4000);
|
|
||||||
} catch (e) { toast('下载失败:' + name + ' ' + e.message); }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async function bulkDelete() {
|
async function bulkDelete() {
|
||||||
|
|||||||
Reference in New Issue
Block a user