我正在尝试获取并将图像高度和图像宽度分配给隐藏在 html 中的输入类型文本,如下所示:目的是获取高度和宽度以检查和验证它们。html:<input type="file" class="file" #introIcon id="uploadBtn" (change)="onFileChange($event)" name="browseIcon"><input type="number" class="hidden" id="imgWidth" value=""><input type="number" class="hidden" id="imgHeight" value="">ts:onFileChange(event) { if (event.target.files.length > 0) { const file = event.target.files[0]; this.fileName = file.name; this.fileType = file.type; if(this.fileType == 'image/png' || this.fileType == 'image/jpeg' || this.fileType == 'image/PNG' || this.fileType == 'image/JPEG') { const reader = new FileReader(); reader.onload = e => { this.url = reader.result; var imageObj = new Image(); imageObj.src = this.url; imageObj.onload = function () { console.log('p1:', imageObj.height); console.log('p2:', imageObj.width); // (<HTMLSelectElement>document.getElementById('imgWidth')).value = imageObj.width; <-- here getting error // (<HTMLSelectElement>document.getElementById('imgHeight')).value = imageObj.height; <-- here getting error }; } reader.readAsDataURL(file); let fName = (<HTMLSelectElement>document.getElementById('uploadFile')).value; fName = this.fileName; this.uploadFileName.nativeElement.value = fName; this.form.get('browseIcon').setValue(file); } else { // error } } }在以下几行中出错:(<HTMLSelectElement>document.getElementById('imgWidth')).value = imageObj.width; (<HTMLSelectElement>document.getElementById('imgHeight')).value = imageObj.height;“数字”类型不能分配给“字符串”类型我已经尝试过谷歌,但没有一个解决方案有效!
1 回答
偶然的你
TA贡献1841条经验 获得超3个赞
怎么样
(<HTMLSelectElement>document.getElementById('imgWidth')).value = imageObj.width.toString();
(<HTMLSelectElement>document.getElementById('imgHeight')).value = imageObj.height.toString();
添加回答
举报
0/150
提交
取消