zouyu
2023-11-17 d8ac6057eaad648687699e25a575f3b7b8c1b102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
export function transform(response) {
  const relType = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel;charset=UTF-8']
  let type = response.data.type
  if (relType.includes(type)) {
    const blob = new Blob([response.data], {type: 'application/vnd.ms-excel'})
    const disposition = response.headers["content-disposition"]
    let temp = disposition.substring(disposition.lastIndexOf('=') + 1)
    let filename = decodeURI(temp)
    // 创建一个超链接,将文件流赋进去,然后实现这个超链接的单击事件
    const elink = document.createElement('a')
    elink.download = filename
    elink.style.display = 'none'
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  }
}