js实现pdf文件路径预览和下载
预览
直接浏览器窗口打开默认就是预览
window.open('文件路径')
下载
function downloadPDF(url, filename) {
fetch(url)
.then(response => response.blob())
.then(blob => {
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch(error => console.error('Error downloading PDF:', error));
}
//调用
downloadPDF('文件路径','文件名.pdf')