elementUI中el-table中使用el-upload

项目中需要在行里加入上传文件,又不好获取scope.row的中参数,差了老多资料解决了
在这里插入图片描述

<template>
   <el-table-column label="操作" width="270" fixed="right">
            <template slot-scope="scope">
              <el-upload
                class="upload-demo"
                ref="upload"
                action="#"
                :data="paramsObj"
                :http-request="
                  (file) => {
                    return uploadFile(file, scope.row);
                  }
                "
                :show-file-list="false"
                :before-upload="beforeUpload"
              >
                <el-button type="text" size="small">上传文件模板</el-button>
              </el-upload>
              <el-button
                type="text"
                size="small"
                @click="changeStatus(scope.row)"
                >{{ scope.row.enabledFlag ? "停用" : "启用" }}</el-button
              >
              <el-button
                @click="updateTemplate(scope.row)"
                type="text"
                size="small"
                >编辑</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="delTemplate(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
</template>
<script>
   //  文件上传
    uploadFile(file, row) {
      this.fileName = file.file.name;
      const formData = new FormData();
      formData.append("file", file.file);
      formData.append("templateId", row.id);
      api.upload(formData).then((res) => {
        if (res.status == 200) {
          this.reaultData = res.data.data;
          this.$message({
            type: "success",
            message: "上传成功",
          });
          this.getTemplateList();
        } else {
          this.$message({
            type: "error",
            message: "上传失败",
          });
        }
      });
    },
    //  文件上传之前的校验
    beforeUpload(file) {
      let fileSize = file.size / 1024 / 1024;
      let isXls =
        file.name.split(".")[file.name.split(".").length - 1] === "xls";
      let isXlsx =
        file.name.split(".")[file.name.split(".").length - 1] === "xlsx";
      if (!isXls && !isXlsx) {
        this.$message.warning("上传文件只能是xls、xlsx格式!");
        return;
      }
      if (fileSize > 10) {
        this.$message.warning("文件超过10MB,请重新上传!");
        return;
      }
    }
</script>
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值