Compare commits

..

No commits in common. "7c741962d4b0456eb3d542d5499481a72f46d07a" and "9cba09c2fb23fbcbaadaab3ec88050d09cb5e9da" have entirely different histories.

2 changed files with 38 additions and 84 deletions

View File

@ -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) { //
// 使fetchblob 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>

View File

@ -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) { //
// 使fetchblob 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>