vue 批量下载文件,不走后端接口的方法

今天ld提了一个需求,说页面的列表里面有要下载的地址,然后点击批量下载。我思索片刻,给出了代码

1.这个是列表页面的代码

javascript 复制代码
<!-- 这个是列表页面的代码 -->
<el-table :data="userListShow" align="center"
           border highlight-current-row size="small" 
           style="width: 100%"
           stripe
            ref="table">
            <el-table-column 
              label="选择"
              width="45px"
              fixed
            >
              <template slot-scope="scope" > 
                <el-checkbox class="biaodiandian"  v-model="scope.row.selected" :label="scope.row" @change="clickChange(scope.row)"><i></i></el-checkbox>
              </template>
            </el-table-column>
            <el-table-column prop="barcode"  width="200px"  show-overflow-tooltip align="center" label="条码号"></el-table-column>
            <el-table-column prop="createTime"  width="200px" show-overflow-tooltip align="center" label="登记时间"></el-table-column>
</el-table>

2.这个是选择框的代码

javascript 复制代码
data(){
    return(){
       selectedRows:[],  //这个是传递的复选框的值,因为是批量选择吗,所以给的是数组
    }
}   


 methods:{

     //选择文件 选择复选框
    clickChange(row) {
      if (row.selected) {
        this.selectedRows.push(row); // 选中时添加到数组中
      } else {
        const index = this.selectedRows.findIndex(item => item === row);
        if (index > -1) {
          this.selectedRows.splice(index, 1); // 取消选中时从数组中移除
        }
      }
    },

  
  }

3.好了,现在该批量下载了,之所以写上面两步,是因为得做批量选择的复选框,也就是得批量拿到数据

javascript 复制代码
<!-- 这个是批量下载的按钮->
<el-button type="warning" @click="handleDownload" round size="mini">下载体检报告</el-button>

4.这个按钮的方法

javascript 复制代码
methods:{


//这个可以直接赋值过去就能用,还有你的浏览器得开启允许批量下载,也就是第一次回
//触发一个提示说,是否允许批量下载多个文件,要点击允许就行了
async handleDownload() {
    //this.selectedRows 这个就是上面写的那个批量拿到的数据
    //row.reportUrl 这个就是列表数据里面的地址路径,
    const reportUrls = this.selectedRows.map(row => row.reportUrl);
    for (const reportUrl of reportUrls) {
      if (reportUrl) { //判断是否存在
        const link = document.createElement('a');
        link.href = reportUrl;
        link.download = reportUrl.substring(reportUrl.lastIndexOf('/') + 1);
        link.style.display = 'none';
        document.body.appendChild(link);
        link.click();
        await new Promise((resolve) => setTimeout(resolve, 500));
        document.body.removeChild(link);
      }
    }
  },

} 

好了兄弟们,到这里就结束了,上面的代码可以直接拿过就能用,不想要上面插件上面依赖的,就是vue的代码。

代码没有走后台的接口

相关推荐
嚣张丶小麦兜4 小时前
认识vite
前端·javascript·vue.js
oak隔壁找我6 小时前
Node.js的package.json
前端·javascript
Awu12276 小时前
Vue3自定义渲染器:原理剖析与实践指南
前端·vue.js·three.js
支撑前端荣耀6 小时前
从零实现前端监控告警系统:SMTP + Node.js + 个人邮箱 完整免费方案
前端·javascript·面试
进击的野人6 小时前
Vue.js 插槽机制深度解析:从基础使用到高级应用
前端·vue.js·前端框架
用户4099322502126 小时前
Vue3 v-if与v-show:销毁还是隐藏,如何抉择?
前端·vue.js·后端
JimmyWhat6 小时前
Vue单页应用路由404问题:服务器配置与Hash模式解决方案
vue.js
shanLion6 小时前
Vite项目中process报红问题的深层原因与解决方案
前端·javascript
烟袅6 小时前
从零构建一个待办事项应用:一次关于组件化与状态管理的深度思考
前端·javascript·react.js
JIngJaneIL6 小时前
基于springboot + vue健康管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端