element ui 文件上传前的限制


除了获取上传文件后缀名称的函数方法这个方法之外 其他都要放在element ui 上传组件的before-upload方法里

格式限制

获取上传文件后缀名称的函数方法

getFileType (name) {
    let startIndex = name.lastIndexOf(".")
    if (startIndex !== -1) {
     return name.slice(startIndex + 1).toLowerCase()
    } else {
     return ""
    }
   },
let suffix = this.getFileType(file.name) //获取文件后缀名
    let suffixArray = ["jpg", "png", "jpeg", "gif", "pdf", "ceb", "word", "excel", "ppt"] //限制的文件类型,根据情况自己定义
    if (suffixArray.indexOf(suffix) === -1) {
     this.$message({
      message: "附件材料格式为jpg/png/jpeg,pdf/ceb,word/excel/ppt",
      type: "error",
      duration: 2000
     })
    }

大小限制

const sizeCheck = file.size / 1024 / 1024 < 20;
    if (!sizeCheck) {
     this.$message.error("单个附件材料大小不能超过 20MB!");
    }
    return sizeCheck;

如果上传的是图片 对图片进行大小比例限制

const isSize = new Promise((resolve, reject) => {
     const width = 4;
     const height = 3;
     const _URL = window.URL || window.webkitURL;
     const img = new Image();
     img.onload = () => {
      // 限制宽高必须为 18*18 像素
      //const valid = img.width == width && img.height == height && img.width === img.height;
      // // 限制宽高必须为 1:1 比例
      //const valid = img.width == img.height;
      //按照一定比例 本例子为4:3
      const valid = img.width / img.height === width / height
      // // 限制必须为竖屏图片(宽必须小于高)
      // const valid = img.width < img.height;
      // // 限制必须为横屏图片(宽必须大于高)
      // const valid = img.width > img.height;
      valid ? resolve() : reject();
     };
     img.src = _URL.createObjectURL(file);
    }).then(
     () => {
      return file;
     },
     () => {
      this.$message.error('上传图标尺寸限制为4*3比例');
      return Promise.reject();
     },
    );
    return isSize
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值