fix file down
This commit is contained in:
parent
22e9b74173
commit
44d3555e06
@ -357,7 +357,6 @@ const downloadPdf = (url) => {
|
|||||||
fileName = fileName.split(/[?_]/)[0];
|
fileName = fileName.split(/[?_]/)[0];
|
||||||
}
|
}
|
||||||
link.download = fileName;
|
link.download = fileName;
|
||||||
link.target = "_blank";
|
|
||||||
|
|
||||||
// 对于移动设备,我们需要特殊处理
|
// 对于移动设备,我们需要特殊处理
|
||||||
const isMobile =
|
const isMobile =
|
||||||
@ -366,12 +365,15 @@ const downloadPdf = (url) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
// 在移动设备上,可能需要使用fetch下载文件并创建blob
|
// 在移动设备上,使用fetch下载文件并直接触发下载,不使用blob URL进行预览
|
||||||
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();
|
||||||
|
|
||||||
@ -383,11 +385,16 @@ const downloadPdf = (url) => {
|
|||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error("下载文件时出错:", error);
|
console.error("下载文件时出错:", error);
|
||||||
// 如果fetch失败,回退到window.open
|
// 如果fetch失败,尝试直接下载
|
||||||
window.open(url, "_blank");
|
link.href = url;
|
||||||
|
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,7 +357,6 @@ const downloadPdf = (url) => {
|
|||||||
fileName = fileName.split(/[?_]/)[0];
|
fileName = fileName.split(/[?_]/)[0];
|
||||||
}
|
}
|
||||||
link.download = fileName;
|
link.download = fileName;
|
||||||
link.target = "_blank";
|
|
||||||
|
|
||||||
// 对于移动设备,我们需要特殊处理
|
// 对于移动设备,我们需要特殊处理
|
||||||
const isMobile =
|
const isMobile =
|
||||||
@ -366,12 +365,15 @@ const downloadPdf = (url) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
// 在移动设备上,可能需要使用fetch下载文件并创建blob
|
// 在移动设备上,使用fetch下载文件并直接触发下载,不使用blob URL进行预览
|
||||||
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();
|
||||||
|
|
||||||
@ -383,11 +385,16 @@ const downloadPdf = (url) => {
|
|||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error("下载文件时出错:", error);
|
console.error("下载文件时出错:", error);
|
||||||
// 如果fetch失败,回退到window.open
|
// 如果fetch失败,尝试直接下载
|
||||||
window.open(url, "_blank");
|
link.href = url;
|
||||||
|
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