前端js通过a标签直接预览pdf文件,弹出下载页面问题
某些浏览器(如Chrome)会优先尝试打开PDF文件,即使设置了download属性 。此时可以通过设置响应头强制触发下载,如果是get请求的话,直接可以在后面添加?response-content-disposition=attachment参数:
// 下载
const onDownload = (record) => {const aLink = document.createElement("a");aLink.style.display = "none";aLink.href = ${record.filePath}?response-content-disposition=attachmentaLink.download = record.fileNamedocument.body.appendChild(aLink);aLink.click();aLink.remove();
}