简体   繁体   English

无法将图像高度和宽度分配给打字稿中的输入类型框

[英]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.

相关问题 如何根据输入框中的值更改图像的宽度和高度? - How to change the width and height of image based on values from input box? 无法在javascript中获取图像的高度和宽度 - Can't get height and width of image in javascript 为什么我不能将打字稿泛型类型分配给该类型的联合? - Why can't I assign a typescript generic type to a union of that type? 打字稿无法为具有 hasOwnProperty 类型缩小的对象键赋值 - Typescript can't assign value to object key with hasOwnProperty type narrowing 使用 TypeScript 获取 Angular 中图像的高度和宽度 - Getting the height and width of an image in Angular using TypeScript 为什么不能设置Image对象的宽度和高度? - Why can't I set the width and height for my Image object? Reactjs 和 typescript 将随机生成的变量分配给 div 样式的宽度和高度 - Reactjs and typescript assign randomly generated variable to div style width and height 不能在打字稿上分配减速器? - can't assign reducer on typescript? 无法在 Typescript 中为字符串分配任何类型,但我正在尝试分配字符串值 - Can't assign type any to string in Typescript, but i am trying to assign a string value 在输入栏上限制图像的宽度和高度 - Limit width and height of image on input field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM