elementUI上传图片限制图片格式,大小和尺寸问题

//上传图片前的图片验证回调
beforeAvatarUpload(file) {
    //图片格式
	const isJPG = file.type === 'image/jpg' || file.type === 'image/png';
    //图片大小
	const isLt2M = file.size / 1024 / 1024 < 2;
	if (!isJPG) {
		this.$message.error('上传图片只能为jpg或png格式');
	}
	if (!isLt2M) {
		this.$message.error('上传图片大小不能超过2MB');
	}
	const _this = this;
	const isSize = new Promise(function(resolve, reject) {
		const img = new Image();
		const _URL = window.URL || window.webkitURl;
		img.onload = function() {
			file.width = img.width;//图片宽度
			file.height = img.height;//图片高度
			const valid = img.width === 1920 && img.height === 600;//上传图片尺寸判定
			valid ? resolve() : reject(new Error('error'));
		};
		img.src = _URL.createObjectURL(file);
	}).then(
		() => {return file;},
		() => {
				_this.$message.error('上传图片尺寸必须为1920*600');
				return Promise.reject(new Error('error'));
			  }
		);
		return isJPG && isLt2M && isSize;
	},

 

handleAvatarSuccess(res, file){
      let front = file.response.fileId.substr(0, 3);
//拼接图片地址
      this.applyForm.coverImg = `自己的网站域名/${front}/${file.response.fileId}.${file.response.extName}`;
    },

 


版权声明:本文为qq_39561207原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。