vue项目this.$refs唤起el-upload组件

<template>

  <section>

    <el-button v-if="fileName" type="text" @click="handelReplace" class="l-h-20">重新上传</el-button>

    <el-upload

      v-show="!fileName"

      class="upload-wrap m-r-5"

      ref="uploadBox"

      accept=".xlsx, .xls"

      :format="['xlsx','xls']"

      :on-format-error="handleFormatError"

      :before-upload="handelBeforeUpload"

      :on-remove="handelRemove"

      :http-request="uploadSectionFile"

      :show-file-list="false"

      :limit="1"

      drag

    >

      <div>将文件拖拽至此区域</div>

      <el-button type="text">上传文件</el-button>

    </el-upload>

  </section>

</template>

 

<script>

export default {

  data() {

    return {

      uploadURL: 'www.baidu.com',

      fileName: "",  // 文件名

      updateData: {} // 入参

    };

  },

  methods: {

    getExtension(fName) {

      return /[.]/.exec(fName) ? /[^.]+$/.exec(fName)[0] : undefined;

    },

    handelBeforeUpload(file) {

      let ext = this.getExtension(file.name);

      if (ext !== "xls" && ext !== "xlsx") {

        this.$message.error("请上传execl文件!");

        this.fileName = "";

        return false;

      } else {

        this.fileName = file.name;

      }

    },

    handelRemove(file, fileList) {

      this.fileName = "";

      this.updateData = {};

    },

    handleFormatError(file) {

      this.$message.error("文件格式不正确,请上传.xls,.xlsx文件。");

    },

    uploadSectionFile(params) {

      let reader = new FileReader();

      reader.readAsDataURL(params.file);

      reader.onload = async () => {

        this.updateData = {

          base64: reader.result.split(",")[1],

          ticket: "base64"

        };

        let { data, errmsg } = await this.$api.apiUrl(

          this.updateData

        );

        if (data) {

          this.$message.success("导入成功");

        } else {

          this.$message.error(errmsg);

        }

      };

    },

    handelReplace() {

      // 重新上传

      this.fileName = "";

      this.updateData = {};

      if (this.$refs.uploadBox) {

        this.$refs.uploadBox.clearFiles();

        this.$refs.uploadBox.$children[0].$refs.input.click();

      }

    }

  }

};

</script>

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值