el-upload组件限制上传图片尺寸大小
<el-upload
ref="img"
action=""
:limit="1"
:auto-upload="false"
accept=".jpg,.jpeg,.png,.gif"
list-type="picture-card"
:on-change="($event) => onFileChange($event, 'img')"
:on-remove="($event) => onFileRemove($event, 'img')"
:on-exceed="($event) => onFileExceed($event, 'img')"
>
<i class="el-icon-plus"></i>
</el-upload>
checkImageSize(file, fileType) {
const reader = new FileReader(); reader.onload = (e) => { const img = new Image(); img.onload = () => { const { width, height } = img; // 设置你希望限制的图片尺寸 const MAX_WIDTH = 107; const MAX_HEIGHT = 121; if (width === MAX_WIDTH && height === MAX_HEIGHT) { this.adForm[fileType] = file.raw this.$refs['adForm'].validateField(fileType) this.$message.success(`图片尺寸符合要求: ${width}x${height}`); } else { this.$message.error(`图片尺寸不能超过 ${MAX_WIDTH}x${MAX_HEIGHT}`); // 清空当前上传的文件 this.adForm[fileType] = '' this.$refs['adForm'].validateField(fileType) } }; img.src = e.target.result; }; reader.readAsDataURL(file.raw); },
onFileChange: function(file, fileType) { // 图片上传限制尺寸 console.log('file, fileType====',file, fileType); this.checkImageSize(file, fileType); },