javascript 下载方法

  • javascript 下载方法
import axios from '@/common/services/axios-instance'
function download(url, fname) {
  return axios({
    url,
    responseType: 'blob',
  }).then((res) => {
    if (flag) ProgressBar.done()
    if ('msSaveOrOpenBlob' in window.navigator) {
      // 兼容IE
      window.navigator.msSaveOrOpenBlob(res.data, fname)
      return
    }
    const blobUrl = URL.createObjectURL(res.data)
    const a = document.createElement('a')
    a.href = blobUrl
    a.download = fname
    // 不用a.click()因为52的firefox不允许
    var evt = document.createEvent('MouseEvents')
    evt.initEvent('click', true, true)
    a.dispatchEvent(evt)
    URL.revokeObjectURL(blobUrl)
  })
}
export default download

你可能感兴趣的:(前端爬坑,javascript)