vue项目下载zip压缩包

直接上代码

1.请求后端接口

//必须加上responseType,因为下载的zip属于流和普通的下载方式还是有较大区别的,同时headers也需要设置一下,不然是下载不了。
export function loadLabel(query) {
  return request({
    url: '/express/admin/loadLabel',
    method: 'get',
    params: query,
    responseType: 'blob',
    headers:{ 'Content-Type': 'application/json; application/octet-stream'},
  })
}

2.HTML

<el-button
    size="mini"
    type="text"
    icon="el-icon-download"
    @click="handleIndent(scope.row)"
    v-hasPermi="['express:result:export']">
      下载失败订单
</el-button>

3.JS

  //下载面单封装方法
    fileHandle(data, fileName) {
      let blob = new Blob([data], { type: 'application/zip' })
      let url = window.URL.createObjectURL(blob)
      const link = document.createElement('a') // 创建a标签
      link.href = url
      link.download = fileName // 重命名文件
      link.click()
      URL.revokeObjectURL(url) // 释放内存
    },
    /** 下载面单 */
    handleSheet(row){
      this.queryParams.parentId=row.id,
      this.queryParams.statusCode=200
      this.$confirm('是否确认下载成功的面单嘛?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading1= this.$loading({
          lock: true,
          text: '正在下载,请稍等',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        loadLabel(this.queryParams).then(res => {
          loading1.close();
          this.fileHandle(res,'面单.zip')
        })
      }).catch(() => {
      });
    },

当然也可以不用封装的方法

 handleSheet(row){
      this.queryParams.parentId=row.id,
      this.queryParams.statusCode=200
      const queryparmas=this.queryParams
      this.$confirm("是否确认导出所有数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function() {
        return loadLabel(queryparmas);
      }).then(response => {
        let blob = new Blob([response.data], {type: 'application/zip'})
        let url = window.URL.createObjectURL(blob)
        const link = document.createElement('a') // 创建a标签
        link.href = url
        link.download = '面单.zip' // 重命名文件
        link.click()
        URL.revokeObjectURL(url) // 释放内存
      }).catch(function(err) {
        console.log(err)
      });
    },

以上两种形式的代码就能实现zip下载

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿wei程序媛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值