element-upload 上传图片限制文件格式、大小以及隐藏上传框

24 篇文章 0 订阅
14 篇文章 1 订阅

废话不多说 先看效果
在这里插入图片描述
代码如下:

<template>
  <div>
    <div>
      <el-upload
        class="avatar-uploader"
        :action="upUrl"
        :on-success="handleAvatarSuccess"
        list-type="picture-card"
        :before-upload="beforeAvatarUpload"
        :on-preview="handlePictureCardPreview"
        :class="{ disabled: isMax }"
        :on-change="change"
        :on-remove="remove"
        :on-error="error"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      upUrl: "http://xxxxxxx:8097/admin/oss/upload/image", //上传地址 必填 (process.env.VUE_APP_BASE_API + '/oss/upload/image')
      isMax: false,
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  created() {},
  mounted() {},
  methods: {
    //文件上传成功时的钩子
    handleAvatarSuccess(res, file) {
      console.log("文件上传成功", res.data);
    },
    //上传文件之前的钩子,参数为上传的文件,可用作文件大小类型的判断,若返回 false 或者返回 Promise 且被 reject,则停止上传。	function(file)
    beforeAvatarUpload(file) {
      console.log("文件", file);
      // 判断图片格式
      const isType =
        file.type === "image/png" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/gif";
      // 判断上传图片大小
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isType) {
        this.$message.error("上传图标只能为 jpg、png、gif、jpeg格式!!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
        return false;
      }
      return isType && isLt2M;
    },
    //文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用	function(file, fileList)
    change(file, fileList) {
      console.log("文件改变change");
      if (fileList.length >= 3) {
        this.isMax = true;
      }
    },
    //文件列表移除文件时的钩子 function(file, fileList)
    remove(file, fileList) {
      console.log("remove");
      if (fileList.length < 3) {
        this.isMax = false;
      }
    },
    //上传失败的时候钩子函数  会出现Bug,故在上传失败的时候也进行一下判断 function(err, file, fileList)
    error(err, file, fileList) {
      this.$message.error("上传失败");
      if (fileList.length >= 3) {
        this.isMax = true;
      } else {
        this.isMax = false;
      }
    },
    // 点击文件列表中已上传的文件时的钩子 function(file)
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  },
};
</script>

<style scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #5d5656;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
::v-deep .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
::v-deep .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
::v-deep .disabled .el-upload--picture-card {
  display: none;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值