handleBeforeUpload(file) { let _this = this return new Promise((resolve, reject) => { const check = _this.uploadList.length < _this.mutileUploadData.maxNum if (!check) { this.$Notice.warning({ title: ‘最多上传‘ + this.mutileUploadData.maxNum + ‘张‘ }) reject(false)//重点 } debugger let image = new Image() image.src = URL.createObjectURL(file) image.onload = () => { let width = _this.mutileUploadData.maxWidth let height = _this.mutileUploadData.maxHeight if ( (width && image.width > width) || (height && image.height > height) ) { this.$Modal.warning({ title: ‘图片尺寸‘, content: ‘图片尺寸 ‘ + file.name + ‘ 太大,不能超过‘ + this.mutileUploadData.maxWidth + ‘*‘ + this.mutileUploadData.maxHeight + ‘。‘, onOk() {} }) reject(false)//重点 } resolve(true)//重点 } image.onerror = () => { reject() } }) }
为什么要用Promise呢,因为image.onload()方法异步,