// 方式一
let link = document.createElement("a");
link.href = url + "?response-content-type=application%2Foctet-stream";
link.target = "_blank";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// 方式二 使用blob进行二进制数据流处理 进行下载
fetch(url)
.then(response => response.blob()) .then(blob => {
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = '项目指标';
link.click();
window.URL.revokeObjectURL(url);
}) .catch(error => {
console.error('错误:', error);
})
方式一的文件名无法通过link.download = 'xxxxx.xlsx'进行修改
OSS返回的Content-Disposition头优先级高于download属性
需在OSS链接添加参数覆盖响应头:
link.href = ${url}?response-content-disposition=attachment%3Bfilename%3D${encodeURIComponent('xxxxx.xlsx')}