js 通过window.open(url)下载文件,修改文件名

通过window.open(url)下载文件(xlsx、xls、zip等格式文件),如果前端想要自定义更改下载时的文件名,可以使用以下方法

// 下载文件,自定义文件名称
export function downFile(url, fileName) {
  const x = new XMLHttpRequest()
  x.open('GET', url, true)
  x.responseType = 'blob'
  x.onload = function() {
    const url = window.URL.createObjectURL(x.response)
    const a = document.createElement('a')
    a.href = url
    a.download = fileName
    a.click()
  }
  x.send()
}
 

在页面调用

downFile('url', '自定义文件名')

猜你喜欢

转载自blog.csdn.net/weixin_43923808/article/details/130628236