js下载文件方法

2023-02-16  本文已影响0人  Frank_Fang
// 通过文件流下载
static downloadFile (data, fileName) {
  const a = document.createElement('a')
  document.body.appendChild(a)
  a.style = 'display: none'
  const blob = new Blob([data], {
    type: 'application/octet-stream'
  })
  const url = window.URL.createObjectURL(blob)
  a.href = url
  a.download = fileName
  a.click()
  window.URL.revokeObjectURL(url)
}

// 通过url进行下载
static downloadUrlFile (url, fileName) {
  const a = document.createElement('a')
  document.body.appendChild(a)
  a.style = 'display: none'
  a.href = url
  a.download = fileName
  a.click()
  window.URL.revokeObjectURL(url)
}
上一篇 下一篇

猜你喜欢

热点阅读