mirror of
https://github.com/RhenCloud/Cloud-Index.git
synced 2025-12-06 15:26:10 +08:00
feat: 优化下载功能,支持路径编码和直接下载链接
This commit is contained in:
@@ -12,7 +12,12 @@ function attachDownloadButtonListeners() {
|
||||
button.addEventListener("click", () => {
|
||||
const key = button.dataset.downloadKey;
|
||||
const name = button.dataset.downloadName;
|
||||
downloadFile(`/download/${key}`, name);
|
||||
// 对路径分段编码,保留路径分隔符,避免 # ? 等字符破坏 URL
|
||||
const encoded = key
|
||||
.split("/")
|
||||
.map((seg) => encodeURIComponent(seg))
|
||||
.join("/");
|
||||
downloadFile(`/download/${encoded}`, name);
|
||||
});
|
||||
button.dataset.listenerAttached = "true";
|
||||
}
|
||||
|
||||
@@ -166,31 +166,19 @@ function downloadFile(url, filename) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.startsWith("/download/")) {
|
||||
fetch(url)
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.blob();
|
||||
})
|
||||
.then((blob) => {
|
||||
const link = document.createElement("a");
|
||||
const blobUrl = URL.createObjectURL(blob);
|
||||
link.href = blobUrl;
|
||||
link.download = filename || "file";
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(blobUrl);
|
||||
if (url.startsWith("/download/") || url.startsWith("/file/")) {
|
||||
// 让浏览器原生跟随服务器重定向(OneDrive 直链/共享链接),避免 fetch 对 3xx 的处理差异
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.target = "_blank";
|
||||
link.rel = "noopener";
|
||||
|
||||
const statusDiv = updateStatus(`✓ 开始下载: ${filename || ""}`, "success");
|
||||
hideStatusLater(statusDiv);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Download error:", error);
|
||||
updateStatus(`✗ 下载失败: ${error.message}`, "error");
|
||||
});
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
|
||||
const statusDiv = updateStatus(`✓ 开始下载: ${filename || ""}`, "success");
|
||||
hideStatusLater(statusDiv);
|
||||
} else {
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
|
||||
Reference in New Issue
Block a user