透過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()
}