VUE实现上传附件(各类都支持)并能删除

 通常上传都分为上传图片和文件,那如果要求都能上传,以下的代码一定能帮到你:

<div style="padding:20px">
      <div class="body">
        <p>请打包上传授信资料清单及各模板资料</p>
        <br>
        <div>
          <p style="float:left;color:red;font-size:14px">*</p>
          <p style="float:left;font-size:14px">申请授信资料上传</p>
          <br>
        </div>
        <br>
        <el-upload
          class="upload-demo"
          ref="classZip"
          action="action"
          :data="{ taskId: helloTaskId }"
          :on-preview="handlePreview"
          name="classZip"
          :on-remove="handleRemove"
          :on-error="handleError"
          :on-success="handleSuccess"
          multiple
          :on-exceed="handleExceed"
          :before-upload="beforeAvatarUpload"
          :limit="1"
          :on-change="handleChange"
          :auto-upload="false"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <div slot="tip" class="el-upload__tip" style="color: red;">
            {{ message }}
          </div>
        </el-upload>
        <!-- <button
          class="helloSubmitBtn"
          @click="submitUpload"
          :class="
            helloTaskId != '' && wordFlag == true
              ? 'subMitError'
              : 'subMitSuccess'
          "
        >
          提交
        </button> -->
      </div>
    </div>


export default {
  data() {
    return {
      sunmitShow: false,
      helloTaskId: "",
      imgFlag: false,
      message: "",
      Wordmessage: "", //输入不能为空提示
      fileNmae: "",
      wordFlag: false
    };
  },
  methods: {
    handleChange() {
      this.wordFlag = true;
    },
    handleExceed(files, fileList) {
      //  console.log(files,fileList,"文件超出个数限制时的钩子");
      this.message = "只允许上传一个文件";
    },
    upload() {},
    beforeAvatarUpload(file) {
      var FileExt = file.name.replace(/.+\./, "");
      if (["zip", "rar", "gz", ".apk"].indexOf(FileExt.toLowerCase()) === -1) {
        this.message = "文件格式有误,请重新上传";
        return false;
      }

      this.message = "上传中,请稍等";
    },
    handleError(err, file, fileList) {
      //  console.log(err, file, fileList,"失败")
      this.message = "上传失败";
    },
    handleSuccess(response, file, fileList) {
      //console.log(response, file, fileList,"成功")

      this.message = "上传成功";
      setTimeout(() => {
        this.$router.push({
          path: "/homePage",
          query: {
            id: 1
          }
        });
      }, 1000);
    },
    helloSunmitBtn() {},
    submitUpload() {
      //  console.log(this.$refs.classZip)
      if (!this.wordFlag) {
        this.message = "请上传文件";
        return;
      }
      if (this.helloTaskId == "") {
        this.Wordmessage = "请填写任务ID";
        return;
      } else {
        this.Wordmessage = "";
        this.$refs.classZip.submit();
      }
    },
    handleRemove(file, fileList) {
      // console.log(file, fileList,"删除");
    },
    handlePreview(file) {
      //  console.log(file,"已上传的文件时的钩子");
    }
  }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值