Compare commits
No commits in common. "b06317e5819a258a19ceb5d212d6d34917343987" and "97fc7d966bd98ae37c89253da3d8356957bd53d5" have entirely different histories.
b06317e581
...
97fc7d966b
@ -357,6 +357,7 @@ const downloadPdf = (url) => {
|
|||||||
fileName = fileName.split(/[?_]/)[0];
|
fileName = fileName.split(/[?_]/)[0];
|
||||||
}
|
}
|
||||||
link.download = fileName;
|
link.download = fileName;
|
||||||
|
link.target = "_blank";
|
||||||
|
|
||||||
// 对于移动设备,我们需要特殊处理
|
// 对于移动设备,我们需要特殊处理
|
||||||
const isMobile =
|
const isMobile =
|
||||||
@ -365,15 +366,12 @@ const downloadPdf = (url) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
// 在移动设备上,使用fetch下载文件并直接触发下载,不使用blob URL进行预览
|
// 在移动设备上,可能需要使用fetch下载文件并创建blob
|
||||||
fetch(url)
|
fetch(url)
|
||||||
.then((response) => response.blob())
|
.then((response) => response.blob())
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const objectUrl = URL.createObjectURL(blob);
|
const objectUrl = URL.createObjectURL(blob);
|
||||||
link.href = objectUrl;
|
link.href = objectUrl;
|
||||||
// 确保设置download属性并且不设置target="_blank"以避免预览
|
|
||||||
link.download = fileName;
|
|
||||||
link.target = "_self"; // 避免在新窗口打开
|
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
|
|
||||||
@ -385,16 +383,11 @@ const downloadPdf = (url) => {
|
|||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error("下载文件时出错:", error);
|
console.error("下载文件时出错:", error);
|
||||||
// 如果fetch失败,尝试直接下载
|
// 如果fetch失败,回退到window.open
|
||||||
link.href = url;
|
window.open(url, "_blank");
|
||||||
link.download = fileName;
|
|
||||||
link.target = "_self";
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 桌面设备上直接点击链接下载
|
// 桌面设备上直接点击链接
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
document.body.removeChild(link);
|
document.body.removeChild(link);
|
||||||
|
@ -357,6 +357,7 @@ const downloadPdf = (url) => {
|
|||||||
fileName = fileName.split(/[?_]/)[0];
|
fileName = fileName.split(/[?_]/)[0];
|
||||||
}
|
}
|
||||||
link.download = fileName;
|
link.download = fileName;
|
||||||
|
link.target = "_blank";
|
||||||
|
|
||||||
// 对于移动设备,我们需要特殊处理
|
// 对于移动设备,我们需要特殊处理
|
||||||
const isMobile =
|
const isMobile =
|
||||||
@ -365,15 +366,12 @@ const downloadPdf = (url) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
// 在移动设备上,使用fetch下载文件并直接触发下载,不使用blob URL进行预览
|
// 在移动设备上,可能需要使用fetch下载文件并创建blob
|
||||||
fetch(url)
|
fetch(url)
|
||||||
.then((response) => response.blob())
|
.then((response) => response.blob())
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const objectUrl = URL.createObjectURL(blob);
|
const objectUrl = URL.createObjectURL(blob);
|
||||||
link.href = objectUrl;
|
link.href = objectUrl;
|
||||||
// 确保设置download属性并且不设置target="_blank"以避免预览
|
|
||||||
link.download = fileName;
|
|
||||||
link.target = "_self"; // 避免在新窗口打开
|
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
|
|
||||||
@ -385,16 +383,11 @@ const downloadPdf = (url) => {
|
|||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error("下载文件时出错:", error);
|
console.error("下载文件时出错:", error);
|
||||||
// 如果fetch失败,尝试直接下载
|
// 如果fetch失败,回退到window.open
|
||||||
link.href = url;
|
window.open(url, "_blank");
|
||||||
link.download = fileName;
|
|
||||||
link.target = "_self";
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 桌面设备上直接点击链接下载
|
// 桌面设备上直接点击链接
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
document.body.removeChild(link);
|
document.body.removeChild(link);
|
||||||
|
Loading…
Reference in New Issue
Block a user