前端下载文件重命名

前端下载文件处理文件名称,进行重命名的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
export const downloadFile = async (url, filename) => {
const a = document.createElement('a')
url = url.replace('http:', location.protocol)
a.href = await toDataUrl(url)
if (filename.indexOf('.') === -1) {
const filenameSuffix = url.split('.').pop()
filename = filename + `.${filenameSuffix}`
}
a.download = filename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}

export const toDataUrl = url => {
return fetch(url)
.then(response => response.blob())
.then(blob => URL.createObjectURL(blob))
}


前端下载文件重命名
https://peterzhanghui.github.io/2021/12/02/前端下载文件重命名/
作者
前端嘉嘉
发布于
2021年12月2日
许可协议