通过url单文件下载, 并且解决a.download修改文件名不生效问题
/**
* 通过url单文件下载, 并且解决a.download修改文件名不生效问题
* @param {String} url 文件url
* @param {String} filename 文件自定义名称
* @returns Promise
*/
export function downloadFileByUrl(url, filename) {
const x = new XMLHttpRequest()
x.open('GET', url, true)
x.responseType = 'blob'
x.onload = () => {
const url = window.URL.createObjectURL(x.response)
const a = document.createElement('a')
a.href = url
a.target = '_blank'
if (filename) {
a.download = filename
}
a.click()
a.remove()
}
x.send()
}
标签:文件,const,url,filename,download,js,链接,下载 From: https://www.cnblogs.com/hong1/p/18474244