javascript - 无法将图像高度和宽度分配给 typescript 中的输入类型框

标签 javascript image typescript angular7 getelementbyid

我正在尝试获取图像高度和图像宽度并将其分配给隐藏在 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;

Type 'number' is not assignable to type 'string'

我已经尝试过谷歌,但没有一个解决方案有效!

尝试过这个:Type 'number' is not assignable to type 'string'

最佳答案

怎么样

(<HTMLSelectElement>document.getElementById('imgWidth')).value = imageObj.width.toString();  
(<HTMLSelectElement>document.getElementById('imgHeight')).value = imageObj.height.toString();

关于javascript - 无法将图像高度和宽度分配给 typescript 中的输入类型框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57720923/

相关文章:

javascript - 如何使用标记的 URL [VBA]

python - 如何减少此包含 2 个 for 循环的代码的运行时间?

android - 如何在android中为按钮添加图像?

javascript - 在显示预加载器的同时预加载所有 CSS 图像

typescript - 子组件事件广播给父组件

javascript - TypeScript 正在添加 Object.defineProperty(exports, "__esModule", { value : true }); throwing an error

javascript - 通过jquery制作元素 "css-absolute"时奇怪的跳转

javascript - 如何用 selenium 更改 phantomjs 的日志文件路径?

javascript - Slickgrid 编辑器完成事件

angular - 当绑定(bind)到的值不改变时强制进行变化检测