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