• Vue 上传前获取图片宽度尺寸和大小


    参考资料:https://www.cnblogs.com/wyx-remove-love/p/wyx-20190626-1.html

           https://blog.csdn.net/qq_22771739/article/details/87007191

    废话不多说,直接上代码了

       beforeAvatarUpload(file) {
          const isImg = file.type === 'image/jpeg' || file.type === 'image/png'
          const isLt2M = file.size / 1024 / 1024 < 5
    
          if (!isImg) {
            this.$notify({
              title: '失败',
              message: '上传图片只能是 JPG/JPEG/PNG 格式!',
              type: 'error',
              duration: 3000
            })
            return false
          }
          if (!isLt2M) {
            this.$notify({
              title: '失败',
              message: '上传图片大小不能超过 4MB!',
              type: 'error',
              duration: 3000
            })
            return false
          }
    
          var reader = new FileReader();
          reader.readAsDataURL(file);
          reader.onload = function () { //让页面中的img标签的src指向读取的路径 
            var img = new Image()
            img.src = reader.result
            if (img.complete) {//如果存在浏览器缓存中
              if (img.width > 540 || img.height > 300) {
                this.$notify({
                  title: '失败',
                  message: '上传图片分辨率建议540*300,宽度不可超过540px,高度不超过300px!',
                  type: 'error',
                  duration: 3000
                })
                return false
              }
    
            } else {
              img.onload = function () {
                if (img.width > 540 || img.height > 300) {
                  this.$notify({
                    title: '失败',
                    message: '上传图片分辨率建议540*300,宽度不可超过540px,高度不超过300px!',
                    type: 'error',
                    duration: 3000
                  })
                  return false
                }
              }
            }
          }
    
          return isImg && isLt2M
        }
  • 相关阅读:
    js 模块循环加载
    英文
    浏览器
    ecma
    Speaking JavaScript
    es6 exploringjs 2
    es6 兼容性
    es6 中文
    Exploring ES6
    探索ES6(ES2015)
  • 原文地址:https://www.cnblogs.com/dawenyang/p/12369600.html
Copyright © 2020-2023  润新知