vue项目使用element组件上传excel文件

html部分

<button @click="impoortExcel">导入excel文件</button>
    <!-- 导入人员档案 -->
    <!--  action 放的是导入文件的后台地址 -->
    <el-dialog title="导入人员档案" :visible.sync="importDialogVisible" width="66%">
      <el-upload
        ref="upload"
        name="file"
        :limit="limit"
        :auto-upload="false"
        action="接口地址"
        :on-exceed="handleExceed"
        :file-list="filelist"
        :on-change="handleChansge"
      >
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button
          style="margin-left: 10px;"
          size="small"
          type="success"
          @click="postfile"
          :disabled="btn.disable"
        >{{btn.message}}</el-button>
        <div slot="tip" class="el-upload__tip">上传文件只能为excel文件,且为xlsx,xls格式</div>
      </el-upload>
        //此处可以写导入时出现的详细错误信息,包含第几行出现问题显示出来
      <div v-for="o in errmesg" :key="o.message" class="text item">{{ o.message }}</div>
    </el-dialog>

data部分

data() {
  return {  
   importDialogVisible: false,
      //文件
      file: "",
      filename: "",
      limit: 1,
      filelist: [],
      errmesg: [],
      btn: {
        disable: false,
        message: "上传服务器",
      },
   }
}

弹窗方法

impoortExcel() {
      let that = this;
      that.importDialogVisible = true;
    },

选择文件方法

handleExceed(e) {
      // 判断是否只能上传一个文件,超过提示报错
      console.log(e);
      this.$notify.error({
        title: "错误",
        message: "只能上传一个文件哦",
      });
    },
    handleChansge(file, fileList) {
      console.log(file);
      let name = file.name;
      if (!/\.(xlsx|xls|XLSX|XLS)$/.test(file.name)) {
        this.$notify.error({
          title: "错误",
          message: "上传文件只能为excel文件,且为xlsx,xls格式",
        });
        this.filelist = [];
        this.file = "";
        return false;
      }
      this.file = file.raw;
      this.filename = file.name;
    },

上传文件方法

postfile() {
      let that = this;
      if (this.file == "") {
        that.$notify.error({
          title: "错误",
          message: "上传文件不能为空",
        });
        return false;
      } else {
        // 文件形式的需要用 formData形式
        let formData = new FormData();
        formData.append("file", this.file);
        let url = "接口地址";
        let config = {
          headers: { "Content-Type": "multipart/form-data" },
        };
        this.btn.disable = true;
        this.btn.message = "上传中,请等待";
        this.$axios1
          .post(url, formData, config)
          .then(function (response) {
            console.log(response);
            that.$notify({
              title: "成功",
              message: response.data.message,
              type: "success",
            });
            that.filelist = [];
            that.btn.disable = false;
            that.btn.message = "上传服务器";
          })
          .catch((err) => {
            that.btn.disable = false;
            that.btn.message = "上传服务器";
            that.$notify.error({
              title: "错误",
              message: "上传过程出错啦",
            });
          });
      }
    },

效果展示

页面展示


上传成功之后 根据后台返回数据自行处理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值