a标签下载设置文件名
# a标签下载文件
<a href='http://192.168.1.1/abcd.xlsx' download='file.xlsx'>下载</a>
1
# js模拟a标签下载
const downlad = (url: any, fileName: any) => {
const newUrl = url
const link = document.createElement('a')
link.href = newUrl
link.download = fileName
link.target = '_blank'
link.style.display = 'none'
document.body.append(link)
link.click()
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
注意:
- download属性设置fileName只有文件地址与当前地址栏同源才有效
# blob方式进行下载
const downlad = (urls: any, fileName: any) => {
const x = new window.XMLHttpRequest();
x.open('GET', urls, true);
x.responseType = 'blob';
x.onload = () => {
const url = window.URL.createObjectURL(x.response);
const a = document.createElement('a');
a.href = url;
a.target = '_blank'
a.download = fileName;
a.style.display = 'none'
document.body.append(a)
a.click();
};
x.send();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
编辑 (opens new window)
上次更新: 2023/01/30