文件下载

公共方法 

utils 里

export function downloadFile(obj, name, suffix) {
  if (window.navigator.msSaveOrOpenBlob) {
    // 兼容IE
    const blob = new Blob([obj])
    const fileName = parseTime(new Date()) + '-' + name + '.' + suffix
    navigator.msSaveBlob(blob, fileName)
  } else {
    const url = window.URL.createObjectURL(new Blob([obj]))
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    const fileName = parseTime(new Date()) + '-' + name + '.' + suffix
    link.setAttribute('download', fileName)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }
}
 
使用 
import { downloadFile } from '@/utils/index'
 
rechargeLogDownload(params)
        .then((result) => {
          downloadFile(result, '充值记录', 'xlsx')   //  downloadFile(result, '充值记录', 'pdf')
          this.downloadLoading = false
        })
        .catch(() => {
          this.downloadLoading = false
        })
posted @ 2022-12-12 14:42  yorking  阅读(53)  评论(0编辑  收藏  举报