绊夏微凉

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

vue文件下载

请求为get

请求为post

this.$http 自己创建的axios请求

let BASE_URL = this.$http.BASE_URL
const downLoadRequest = this.$http.create({
  baseURL: BASE_URL,
  headers: {
    'Content-Type': 'application/json; charset=utf-8',
    'token': this.$cookie.get('token')
  }
})
downLoadRequest({
  url: '/at/atreceiveprizerecord/export',
  method: 'post',
  data: {
  'drawRecordIds': drawRecordIds,
  'redeemRecordIds': redeemRecordIds,
  'activityName': this.searchForm.activityName,
  'username': this.searchForm.username,
  'drawResult': this.searchForm.drawResult,
  'isCash': this.searchForm.isCash,
  'prizeName': this.searchForm.prizeName,
  'type': type,
  'prizeStatus': this.searchForm.prizeStatus
  },
  responseType: 'blob'
  }).then((res) => {
    console.log(res.data)
    let fileName = new Date().getTime() + '.xls'
    if (window.navigator.msSaveOrOpenBlob) {
      navigator.msSaveBlob(res.data, fileName)
    } else {
      let link = document.createElement('a')
      link.href = window.URL.createObjectURL(res.data)
      link.download = fileName
      link.click()
      // 释放内存
      window.URL.revokeObjectURL(link.href)
      }
  }).catch((e) => {
    this.$message.error('文件下载失败,请联系管理员!!!')
  })
posted on 2021-03-02 10:11  绊夏微凉  阅读(120)  评论(0)    收藏  举报