前端下载文件重命名

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

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))
}


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!