element-ui文件上传---上传表格

注意:element-ui默认封装的传递文件名为file,与后端起的名字要一致

每次只能上传一个文件,多个文件同时上传的话,会多次调用接口,发多次请求

 html:

          <el-form-item label="选择文件:" prop="fileList">
            <el-upload
              class="upload-demo"
              ref="upload"
              action="/mgmt/FinancialDetailMgmtAdd.do"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :file-list="fileList"
              :limit="1"
              :on-exceed="handleExceed"
              accept=".xlsx, .xls"
              :data="params"
              :on-success="handleFileSuccess"
              :auto-upload="false"
              :before-upload="beforeAvatarUpload"
            >
              <el-button slot="trigger" size="small" type="primary"
                >选择文件</el-button
              >
              <el-button
                style="margin-left: 10px;"
                size="small"
                type="success"
                @click="submitUpload"
                >上传</el-button
              >
              <div slot="tip" class="el-upload__tip">
                只能上传xls/xlsx格式文件
              </div>
            </el-upload>
          </el-form-item>

js:

<script>
export default {
  data() {
    return {
      limit: "1",
fileList: [],
};
  },
methods: {
    //  文件上传
    submitUpload(file) {
      this.$refs.upload.submit();
    },
handleRemove(file, fileList) {
      //   console.log(file, fileList);
      this.flag = false;
      this.fileList = [];
    },
    handleExceed(files, fileList) {
      this.$message.warning(`最多上传 ${this.limit} 个文件。`);
    },
    handlePreview(file) {
      //   console.log(file);
    },
    handleFileSuccess(response, file, fileList) {
      console.log("hhhhhhhhhhhhhhh");
      console.log(response.jsonError);

      if (response.jsonError) {
        this.fileList = [];
        this.$message.error(
          `上传失败,${response.jsonError[0]._exceptionMessageCode}`
        );
      } else if (response.state === true) {
        this._JnlNo = response._JnlNo;
        this.fileList.push(file);
        this.flag = true;
        console.log("成功了");
      } else {
        this.fileList = [];
        this.$message.error(`上传失败`);
      }

      // this.upload.open = false;
      // this.upload.isUploading = false;
      // this.$refs.upload.clearFiles();
    },
    beforeAvatarUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      //   console.log(testmsg);
      const extension = testmsg === "xls";
      const extension2 = testmsg === "xlsx";
      // const isLt2M = file.size / 1024 / 1024 < 10;
      if (!extension && !extension2) {
        // console.log(11111111);
        this.$message({
          message: "上传文件只能是 xls、xlsx格式!",
          type: "warning"
        });
      }
      // if (!isLt2M) {
      //   this.$message({
      //     message: "上传文件大小不能超过 10MB!",
      //     type: "warning"
      //   });
      // }
      // return extension || (extension2 && isLt2M);
      return extension || extension2;
    },
}
};
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值