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);
};
const downloadPdf = (url) => {
// a
const link = document.createElement("a");
link.href = url;
const downloadPdf = async (pdfResource, filename = "") => {
try {
// PDF
const response = await fetch(pdfResource);
const blob = await response.blob();
//
let fileName = url.split("/").pop();
//
if (fileName.includes("?") || fileName.includes("_t=")) {
fileName = fileName.split(/[?_]/)[0];
}
link.download = fileName;
link.target = "_blank";
// Blob URL
const blobUrl = URL.createObjectURL(blob);
//
const isMobile =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
//
const a = document.createElement("a");
a.href = blobUrl;
a.download = filename || pdfResource.split("/").pop() || "download.pdf";
if (isMobile) {
// 使fetchblob
fetch(url)
.then((response) => response.blob())
.then((blob) => {
const objectUrl = URL.createObjectURL(blob);
link.href = objectUrl;
document.body.appendChild(link);
link.click();
//
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
//
setTimeout(() => {
document.body.removeChild(link);
URL.revokeObjectURL(objectUrl);
}, 100);
})
.catch((error) => {
console.error("下载文件时出错:", error);
// fetch退window.open
window.open(url, "_blank");
});
} else {
//
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Blob URL
URL.revokeObjectURL(blobUrl);
} catch (error) {
console.error("下载PDF文件失败:", error);
}
};
</script>

View File

@ -345,52 +345,29 @@ const handleSearch = () => {
console.log("搜索:", searchQuery.value);
};
const downloadPdf = (url) => {
// a
const link = document.createElement("a");
link.href = url;
const downloadPdf = async (pdfResource, filename = "") => {
try {
// PDF
const response = await fetch(pdfResource);
const blob = await response.blob();
//
let fileName = url.split("/").pop();
//
if (fileName.includes("?") || fileName.includes("_t=")) {
fileName = fileName.split(/[?_]/)[0];
}
link.download = fileName;
link.target = "_blank";
// Blob URL
const blobUrl = URL.createObjectURL(blob);
//
const isMobile =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
//
const a = document.createElement("a");
a.href = blobUrl;
a.download = filename || pdfResource.split("/").pop() || "download.pdf";
if (isMobile) {
// 使fetchblob
fetch(url)
.then((response) => response.blob())
.then((blob) => {
const objectUrl = URL.createObjectURL(blob);
link.href = objectUrl;
document.body.appendChild(link);
link.click();
//
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
//
setTimeout(() => {
document.body.removeChild(link);
URL.revokeObjectURL(objectUrl);
}, 100);
})
.catch((error) => {
console.error("下载文件时出错:", error);
// fetch退window.open
window.open(url, "_blank");
});
} else {
//
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Blob URL
URL.revokeObjectURL(blobUrl);
} catch (error) {
console.error("下载PDF文件失败:", error);
}
};
</script>