[英]Can't assign image height and width to input type box in typescript
I am trying to get and assign image height and image width to input type text which is hidden in html as below: 我正在尝试获取图像高度和图像宽度并将其分配给输入类型的文本,该文本在html中隐藏,如下所示:
Purpose to get height and width to check and validate them. 目的是获取高度和宽度以进行检查和验证。
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: 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
}
}
}
Getting error in below lines: 在下面的行中出现错误:
(<HTMLSelectElement>document.getElementById('imgWidth')).value = imageObj.width;
(<HTMLSelectElement>document.getElementById('imgHeight')).value = imageObj.height;
Type 'number' is not assignable to type 'string' “数字”类型不能分配给“字符串”类型
I already tried out google but none of the solution worked! 我已经尝试过谷歌,但没有解决方案!
Tried this: Type 'number' is not assignable to type 'string' 尝试过: “数字”类型不能分配给“字符串”类型
How about 怎么样
(<HTMLSelectElement>document.getElementById('imgWidth')).value = imageObj.width.toString();
(<HTMLSelectElement>document.getElementById('imgHeight')).value = imageObj.height.toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.