el-upload判断上传图片的格式与大小

房东的猫 提交于 2019-11-29 06:05:00

给before-upload绑定事件:

判断图片只能是 JPG、GIF、BMP、PNG 格式,且小于5M

beforeUpload(file) {

        let types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png'];

        const isImage = types.includes(file.type);

        const isLtSize = file.size / 1024 / 1024 < 5;

        if (!isImage) {

          this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG 格式!');

          return false;

        }

        if (!isLtSize) {

          this.$message.error('上传图片大小不能超过 5MB!');

          return false;

        }

        return true;

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!