Iview upload 上传文件

1 篇文章 0 订阅
1 篇文章 0 订阅
场景
打开模态框 上传文件, 文件限制 一次一个文件,关闭模态框modal 清除历史文件列表
如图:

在这里插入图片描述

template

在这里插入图片描述

    <Modal v-model="import_modal" title="导入">
      <!-- 去除按钮 自定义页脚 -->
      <div slot="footer"></div>
      <Upload
        ref="upload"
        :action="actionUrl"
        name="excel-file"
        :show-upload-list="true"
        :on-format-error="handleFormatError"
        :on-success="handleSuccess"
        :on-remove="handleRemove"
        :on-error="handleError"
        :before-upload="handleUpload"
        :format="['csv']"
        :data="uploadData"
      >
        <Button type="primary">选择文件</Button> <br />
      </Upload>
      <Button type="primary" style="margin-right:15px;" v-if="total > 0"
        >总条数 : {{ total }}</Button
      >

      <Button type="success" style="margin-right:15px" v-if="success_number > 0"
        >成功 : {{ success_number }}</Button
      >

      <Button type="error" v-if="error_number > 0" @click="downLoadError"
        >下载错误 : {{ error_number }}</Button
      >
    </Modal>
modal

在这里插入图片描述

 // 导入
    handleUpload(file) {
      this.uploadData.file = file;
      const check = this.uploadList.length < 1;
      if (!check) {
        this.$Notice.warning({
          title: "一次一个文件哦",
        });
      }
      return check;
    },
    handleFormatError(file) {
      this.$Notice.warning({
        title: "文件格式不正确",
        desc: "文件 " + file.name + " 格式不正确,请上传.csv文件。",
      });
    },
    handleRemove(file) {
      const fileList = this.$refs.upload.fileList;
      this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
      this.total = 0;
      this.error_number = 0;
      this.success_number = 0;
    },
    handleSuccess(res, file) {
      if (res.code === 0) {
        this.total = res.data.total_count;
        this.error_number = res.data.error_number;
        this.success_number = res.data.success_number;
        this.errorUrl = res.data.url;
        this.file_name = res.data.file_name;
        this.$Message.success("选择文件成功!");
      }
    },
    handleError(error, file) {
      this.$Message.error("数据导入失败!");
    },
mounted

在这里插入图片描述

  mounted() {
    this.uploadList = this.$refs.upload.fileList;
  },

watch

在这里插入图片描述

import_modal(value, oldValue) {
      this.maSearch();
      if (value) {
        this.$refs.upload.clearFiles(); // 清除回显文件列表
        this.uploadList.shift();
        this.uploadList = this.$refs.upload.fileList;
        this.total = 0;
        this.error_number = 0;
        this.success_number = 0;
      }
    },
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值