设置 a 标签的 download属性,可以重置 文件名。如下代码,文件名重置为传入name。
export function fileDownlad({name,url}){
const http = new window.XMLHttpRequest();
http.open('GET', url, true);
http.responseType = 'blob';
http.onload = () => {
const hrefUrl = window.URL.createObjectURL(http.response);
const a = document.createElement('a');
a.href = hrefUrl;
a.target = '_blank'
a.download = name;
a.style.display = 'none'
a.click();
window.URL.revokeObjectURL(a.href);
};
http.send();
}
注意:直接使用 a 标签 也可以下载文件,但是href 的下载地址 和当前网站地址必须是同源的,否则download不生效,无法修改名称。