feat: batch download
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
'use strict';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 批量选择 / 批量下载 (依赖 app.js 的 consts: checkAll, checkAllWrap,
|
||||
// batchBtn, batchCount, filesList, toast)
|
||||
// 用 var 顶层声明, 与 app.js 同 <script> 内共享 (loadFiles / fileRow 读写).
|
||||
// ---------------------------------------------------------------------------
|
||||
var selectedFiles = new Set();
|
||||
|
||||
function onCheckChange(name, checked) {
|
||||
if (checked) selectedFiles.add(name);
|
||||
else selectedFiles.delete(name);
|
||||
updateBatchUI();
|
||||
}
|
||||
|
||||
function updateBatchUI() {
|
||||
const n = selectedFiles.size;
|
||||
batchCount.textContent = n;
|
||||
batchBtn.style.display = n > 0 ? 'inline-block' : 'none';
|
||||
|
||||
const checks = filesList.querySelectorAll('.file-check');
|
||||
if (checks.length === 0) {
|
||||
checkAllWrap.style.display = 'none';
|
||||
checkAll.checked = false;
|
||||
checkAll.indeterminate = false;
|
||||
return;
|
||||
}
|
||||
checkAllWrap.style.display = 'inline-flex';
|
||||
let checkedCount = 0;
|
||||
checks.forEach(c => { if (c.checked) checkedCount++; });
|
||||
checkAll.checked = checkedCount === checks.length;
|
||||
checkAll.indeterminate = checkedCount > 0 && checkedCount < checks.length;
|
||||
}
|
||||
|
||||
checkAll.addEventListener('change', () => {
|
||||
const want = checkAll.checked;
|
||||
filesList.querySelectorAll('.file-check').forEach(c => {
|
||||
if (c.checked !== want) {
|
||||
c.checked = want;
|
||||
const name = c.dataset.name;
|
||||
if (want) selectedFiles.add(name);
|
||||
else selectedFiles.delete(name);
|
||||
}
|
||||
});
|
||||
updateBatchUI();
|
||||
});
|
||||
|
||||
batchBtn.addEventListener('click', async () => {
|
||||
if (selectedFiles.size === 0) return;
|
||||
const names = [...selectedFiles];
|
||||
batchBtn.disabled = true;
|
||||
const restore = () => { batchBtn.disabled = false; batchBtn.textContent = `批量下载 (${selectedFiles.size})`; };
|
||||
batchBtn.textContent = '打包中...';
|
||||
try {
|
||||
const r = await fetch('/download-zip', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ files: names }),
|
||||
});
|
||||
if (!r.ok) {
|
||||
let msg = 'HTTP ' + r.status;
|
||||
try { msg = (await r.json()).error || msg; } catch {}
|
||||
throw new Error(msg);
|
||||
}
|
||||
// 从 Content-Disposition 取 server 起的 zip 文件名, 兜底 'files.zip'
|
||||
let fname = 'files.zip';
|
||||
const cd = r.headers.get('Content-Disposition') || '';
|
||||
const m = cd.match(/filename="?([^";]+)"?/);
|
||||
if (m) fname = m[1];
|
||||
const blob = await r.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url; a.download = fname;
|
||||
document.body.appendChild(a); a.click(); document.body.removeChild(a);
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
toast(`已下载 ${names.length} 个文件`, 'success');
|
||||
} catch (e) {
|
||||
toast('批量下载失败: ' + e.message, 'error');
|
||||
} finally {
|
||||
restore();
|
||||
}
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Boot (从 app.js 末尾移过来, 确保 selectedFiles 已声明再启动首次 loadFiles)
|
||||
// ---------------------------------------------------------------------------
|
||||
loadFiles();
|
||||
setInterval(loadFiles, 30000);
|
||||
Reference in New Issue
Block a user