vue下载模板功能--本地下载

<el-button size="small" type="success" @click="download()">下载模板</el-button>

download() {
  let params = {
    tempType: 'traffic_police'
  }
  this.http({
    method: 'get',
    responseType: 'blob', //防止返回乱码
    // url: '/static/fileExcel/buy.xlsx',//正式
    url: this.baseUrl3 + '/menuPermission/downloadExcel',
    params: params
   }).then(res => {
    if (res.status == '200') {
    // downloadFile (data, name)
    let data = res.data;
    if (!data) {
      this.$message.error('下载失败,解析数据为空!')
    return
    }
    // const datetime = Date.now();
    // 创建一个新的url,此url指向新建的Blob对象
    let url = window.URL.createObjectURL(new Blob([data]))
    // 创建a标签,并隐藏改a标签
    let link = document.createElement('a')
    link.style.display = 'none'
    // a标签的href属性指定下载链接
    link.href = url
    //setAttribute() 方法添加指定的属性,并为其赋指定的值。
    let fileName = '接口模板.xlsx'
    link.setAttribute('download', fileName)
    document.body.appendChild(link)
    link.click()
    // this.$message.success('导出成功');
    this.modal3 = false;
    } else {
      this.$message.error('导出失败,请尝试重新导出');
    }
    });
},

posted on 2020-07-09 11:35  大丸子er  阅读(3508)  评论(0编辑  收藏  举报

导航

{a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+(255*Math.random())+","+(255Math.random())+","+~~(255Math.random())+")"}var d=[];e.requestAnimationFrame=function()}(),n()}(window,document);