Compare commits
No commits in common. "7c741962d4b0456eb3d542d5499481a72f46d07a" and "9cba09c2fb23fbcbaadaab3ec88050d09cb5e9da" have entirely different histories.
7c741962d4
...
9cba09c2fb
@ -345,52 +345,29 @@ const handleSearch = () => {
|
|||||||
console.log("搜索:", searchQuery.value);
|
console.log("搜索:", searchQuery.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const downloadPdf = (url) => {
|
const downloadPdf = async (pdfResource, filename = "") => {
|
||||||
// 创建一个隐藏的a元素
|
try {
|
||||||
const link = document.createElement("a");
|
// 获取PDF文件
|
||||||
link.href = url;
|
const response = await fetch(pdfResource);
|
||||||
|
const blob = await response.blob();
|
||||||
|
|
||||||
// 修复文件名提取逻辑
|
// 创建Blob URL
|
||||||
let fileName = url.split("/").pop();
|
const blobUrl = URL.createObjectURL(blob);
|
||||||
// 移除可能存在的查询参数
|
|
||||||
if (fileName.includes("?") || fileName.includes("_t=")) {
|
|
||||||
fileName = fileName.split(/[?_]/)[0];
|
|
||||||
}
|
|
||||||
link.download = fileName;
|
|
||||||
link.target = "_blank";
|
|
||||||
|
|
||||||
// 对于移动设备,我们需要特殊处理
|
// 创建下载链接
|
||||||
const isMobile =
|
const a = document.createElement("a");
|
||||||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
a.href = blobUrl;
|
||||||
navigator.userAgent
|
a.download = filename || pdfResource.split("/").pop() || "download.pdf";
|
||||||
);
|
|
||||||
|
|
||||||
if (isMobile) {
|
// 触发下载
|
||||||
// 在移动设备上,可能需要使用fetch下载文件并创建blob
|
document.body.appendChild(a);
|
||||||
fetch(url)
|
a.click();
|
||||||
.then((response) => response.blob())
|
document.body.removeChild(a);
|
||||||
.then((blob) => {
|
|
||||||
const objectUrl = URL.createObjectURL(blob);
|
|
||||||
link.href = objectUrl;
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
|
|
||||||
// 清理
|
// 释放Blob URL
|
||||||
setTimeout(() => {
|
URL.revokeObjectURL(blobUrl);
|
||||||
document.body.removeChild(link);
|
} catch (error) {
|
||||||
URL.revokeObjectURL(objectUrl);
|
console.error("下载PDF文件失败:", error);
|
||||||
}, 100);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("下载文件时出错:", error);
|
|
||||||
// 如果fetch失败,回退到window.open
|
|
||||||
window.open(url, "_blank");
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// 桌面设备上直接点击链接
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
@ -345,52 +345,29 @@ const handleSearch = () => {
|
|||||||
console.log("搜索:", searchQuery.value);
|
console.log("搜索:", searchQuery.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const downloadPdf = (url) => {
|
const downloadPdf = async (pdfResource, filename = "") => {
|
||||||
// 创建一个隐藏的a元素
|
try {
|
||||||
const link = document.createElement("a");
|
// 获取PDF文件
|
||||||
link.href = url;
|
const response = await fetch(pdfResource);
|
||||||
|
const blob = await response.blob();
|
||||||
|
|
||||||
// 修复文件名提取逻辑
|
// 创建Blob URL
|
||||||
let fileName = url.split("/").pop();
|
const blobUrl = URL.createObjectURL(blob);
|
||||||
// 移除可能存在的查询参数
|
|
||||||
if (fileName.includes("?") || fileName.includes("_t=")) {
|
|
||||||
fileName = fileName.split(/[?_]/)[0];
|
|
||||||
}
|
|
||||||
link.download = fileName;
|
|
||||||
link.target = "_blank";
|
|
||||||
|
|
||||||
// 对于移动设备,我们需要特殊处理
|
// 创建下载链接
|
||||||
const isMobile =
|
const a = document.createElement("a");
|
||||||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
a.href = blobUrl;
|
||||||
navigator.userAgent
|
a.download = filename || pdfResource.split("/").pop() || "download.pdf";
|
||||||
);
|
|
||||||
|
|
||||||
if (isMobile) {
|
// 触发下载
|
||||||
// 在移动设备上,可能需要使用fetch下载文件并创建blob
|
document.body.appendChild(a);
|
||||||
fetch(url)
|
a.click();
|
||||||
.then((response) => response.blob())
|
document.body.removeChild(a);
|
||||||
.then((blob) => {
|
|
||||||
const objectUrl = URL.createObjectURL(blob);
|
|
||||||
link.href = objectUrl;
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
|
|
||||||
// 清理
|
// 释放Blob URL
|
||||||
setTimeout(() => {
|
URL.revokeObjectURL(blobUrl);
|
||||||
document.body.removeChild(link);
|
} catch (error) {
|
||||||
URL.revokeObjectURL(objectUrl);
|
console.error("下载PDF文件失败:", error);
|
||||||
}, 100);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("下载文件时出错:", error);
|
|
||||||
// 如果fetch失败,回退到window.open
|
|
||||||
window.open(url, "_blank");
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// 桌面设备上直接点击链接
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user