handleDownload() {
const url = "/pdf/user.pfh"; // 替换成实际的PDF文件路径
fetch(url, {
method: "GET",
headers: { "Content-Type": "application/pdf" },
})
.then((response) => response.blob())
.then((blob) => {
const url = window.URL.createObjectURL(new Blob([blob]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "xxx.pdf"); // 设置下载文件的名称
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
})
.catch((error) => {
console.error("Error downloading PDF file: ", error); // 处理下载失败的情况
});
},
前端拿到文件路径,如何直接实现下载,不新开窗口
最新推荐文章于 2024-10-17 16:49:38 发布