From b918ac46508583a0bced1f0fb2aab41c87a43b59 Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 31 Aug 2026 20:23:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E7=AB=AF=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=94=B9=E5=90=8D=20xxx.bin=20=E4=BF=AE=E5=A4=8D=EF=BC=9A?= =?UTF-8?q?=E6=94=B9=E7=94=A8=20data:=20URL=20=E8=A7=A6=E5=8F=91=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=EF=BC=88=E7=A7=BB=E5=8A=A8=E7=AB=AF=20Chrome=20?= =?UTF-8?q?=E5=AF=B9=20blob:=20object=20URL=20=E5=BF=BD=E7=95=A5=20downloa?= =?UTF-8?q?d=20=E5=B1=9E=E6=80=A7=EF=BC=8C=E5=85=9C=E5=BA=95=E6=88=90=20uu?= =?UTF-8?q?id.bin=EF=BC=89=EF=BC=9BJS=20=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=E5=8D=87=E8=87=B3=20v=3D20260831c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/public/index.html | 2 +- app/server/public/js/app.js | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/server/public/index.html b/app/server/public/index.html index 7480755..332ee3f 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -111,6 +111,6 @@ - + diff --git a/app/server/public/js/app.js b/app/server/public/js/app.js index 69499f4..242c060 100644 --- a/app/server/public/js/app.js +++ b/app/server/public/js/app.js @@ -372,13 +372,27 @@ async function downloadFile(name) { let blob = await res.blob(); // 抹掉 blob 的 MIME:响应常为 octet-stream,部分移动端浏览器内核会按类型给下载名追加 .bin,抹掉后按原名保存 try { blob = new Blob([blob], { type: '' }); } catch (_) {} - const dlUrl = URL.createObjectURL(blob); + // 用 data: URL 触发下载:移动端 Chrome(约 114 起)对 blob: object URL 常忽略 download 属性, + // 文件名会兜底成 uuid.bin;data: URL 不走该回归逻辑,能正常保留原文件名。 + let href; + try { + href = await new Promise(function (resolve, reject) { + const r = new FileReader(); + r.onload = function () { resolve(String(r.result)); }; + r.onerror = function () { reject(new Error('blob 转 data URL 失败')); }; + r.readAsDataURL(blob); + }); + } catch (_) { + // 兜底:回到 object URL(电脑端正常) + href = URL.createObjectURL(blob); + setTimeout(function () { URL.revokeObjectURL(href); }, 3000); + } const a = document.createElement('a'); - a.href = dlUrl; + a.href = href; a.download = name.split('/').pop() || 'download'; document.body.appendChild(a); a.click(); - setTimeout(function () { a.remove(); URL.revokeObjectURL(dlUrl); }, 1000); + setTimeout(function () { a.remove(); }, 1000); } catch (e) { toast('下载失败:' + e.message); }