基于elementUI 的上传、下载文件功能

关于上传功能,采用按钮形式,上传Excel文件的功能实现:

<el-upload
     class="upload-demo m-l-10"
     ref="upload"
     accept=".xls, .xlsx"
     :action="update_action_url"
     :on-error="uploadError"
     :on-success="uploadSuccess"
     :before-upload="beforeUpload"
     :show-file-list="false"
>
     <el-button slot="trigger" type="primary" icon="el-icon-upload2">
        上传
     </el-button >
</el-upload>

上传功能常用参数:

action必选参数,上传的地址(api)
before-upload文件上传前的操作,一般对上传格式做处理
on-success文件上传成功时的钩子
on-error文件上传失败时的钩子
accept接受上传的文件类型
show-file-list是否显示已上传文件列表

事件方法:

    uploadError() {
        this.$notify.error({
          title: '上传失败',
          message: '只能上传excel或csv文件'
        });
    },
    uploadSuccess() {
      this.$notify({
          title: '导入成功',
          message: '请刷新查看',
          type: 'success'
        });
      //重新获取数据列表
      this.init();
    },
    beforeUpload(file) {
      // 使用正则判断文件后缀名
      const fileSuffix = file.name.match(/\.[^/.]+$/)[0];
      // 是否存在白名单中
      const whiteList = new Set(".xls", ".xlsx"]);
      if (!whiteList.has(fileSuffix)) return;
    },

常用 下载功能:

window.open("文件地址")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值