angular - 在不使用 jQuery 将图像上传到服务器之前,如何从输入标签获取 Angular 2(或更高版本)图像的高度和宽度?

标签 angular typescript file-upload

我检查了各种来源,但大多数解决方案都在 jQuery 中。如果可能的话,我想要 Typescript 中的解决方案。

HTML -

<input #coverFilesInput class="file-input" type="file"(change)="onChange($event)"....>

typescript -

onChange($event) { let img = event.target.files[0]; // and then I need code to validate image size }

有解决办法还是我做的完全错了?

最佳答案

您可以结合使用 @ViewChild 和 ElementRef 访问文件上传控件并在每次上传后清除它的值,否则 (change) 事件不会触发。

然后您可以使用 FileReader() 将文件读入 Image 对象并从中获取宽度和高度。

这是下面的代码-

HTML 模板

<input type="file" #coverFilesInput (change)="onChange($event)" class="file-input"  />
    Upload Percent: {{percentDone}}% <br />

    <ng-container *ngIf="uploadSuccess">
      Upload Successful of file with size : {{size}} bytes <br>
      The image height is : {{height}} <br>
      The image width is : {{width}} <br>
    </ng-container> 

onChange 方法

onChange(evt:any){
   this.percentDone = 100;
   this.uploadSuccess = true;
   let image:any = evt.target.files[0];
   this.size = image.size;
   let fr = new FileReader();
   fr.onload = () => { // when file has loaded
    var img = new Image();
    img.onload = () => {
        this.width = img.width;
        this.height = img.height;
    };

    img.src = fr.result; // The data URL 
};

  fr.readAsDataURL(image);
   this.imgType.nativeElement.value = ""; // clear the value after upload
  }

完整代码app.component.ts

import { Component, VERSION ,ViewChild,ElementRef} from '@angular/core';
import {HttpClientModule, HttpClient, HttpRequest, HttpResponse, HttpEventType} from '@angular/common/http';

@Component({
  selector: 'my-app',
  template: `
    Version = {{version.full}} <br/>
    <input type="file" #coverFilesInput (change)="onChange($event)" class="file-input"  />
    Upload Percent: {{percentDone}}% <br />

    <ng-container *ngIf="uploadSuccess">
      Upload Successful of file with size : {{size}} bytes <br>
      The image height is : {{height}} <br>
      The image width is : {{width}} <br>
    </ng-container> 
  `,
})
export class AppComponent {
  percentDone: number;
  uploadSuccess: boolean;
  size:any;
  width:number;
  height:number;

  @ViewChild('coverFilesInput') imgType:ElementRef;

  constructor(
    ) { }

  version = VERSION

  onChange(evt:any){
   this.percentDone = 100;
   this.uploadSuccess = true;
   let image:any = evt.target.files[0];
   this.size = image.size;
   let fr = new FileReader();
   fr.onload = () => { // when file has loaded
    var img = new Image();

    img.onload = () => {
        this.width = img.width;
        this.height = img.height;
    };

    img.src = fr.result; // This is the data URL 
   };

  fr.readAsDataURL(image);
   this.imgType.nativeElement.value = "";
  }  
}

这是一个工作演示:https://stackblitz.com/edit/angular-file-upload-hnik7q

编辑:您还可以使用 [(ngModel)]="selectedFile" 访问输入文件控件并在验证和上传完成后清除它的值,无需使用 @ViewChildElementRef 如下 -

<input type="file" #coverFilesInput (change)="onChange($event)" class="file-input"  [(ngModel)]="selectedFile"/>

在组件类中-

export class AppComponent {
  percentDone: number;
  uploadSuccess: boolean;
  size:any;
  width:number;
  height:number;
  selectedFile:any; // declare the property

  constructor(
    ) { }

  version = VERSION

  onChange(evt:any){
   this.percentDone = 100;
   this.uploadSuccess = true;
   let image:any = evt.target.files[0];
   this.size = image.size;
   let fr = new FileReader();
   fr.onload = () => { // when file has loaded
    var img = new Image();    
    img.onload = () => {
        this.width = img.width;
        this.height = img.height;
    };    
    img.src = fr.result; // This is the data URL 
};    
  fr.readAsDataURL(image);
  this.selectedFile = ""; // clear the file here
  }        
}

关于angular - 在不使用 jQuery 将图像上传到服务器之前,如何从输入标签获取 Angular 2(或更高版本)图像的高度和宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50566385/

相关文章:

javascript - Angular2 使用 npm reflect-metadata 或 core-js/es7/reflect

Angular 2 找不到名称 SimpleChanges

javascript - 按照 JavaScript 中给定另一个数组属性的顺序排列数组

Angular 未捕获错误 : Can't resolve all parameters for service

angular - 如何在 Angular 6 项目中使用 svg.js 和插件

json - 遍历复杂的 JSON 对象 - 需要逻辑来获取值的总和

typescript - 理解 TypeScript 2 中的 never 类型

java - 允许用户上传文件

swift - 在 Alamofire 中上传带有深度参数的图像

node.js - 如何测量使用 socket.io 内部传输的数据大小