Angular 2:从文件上传中获取图像

Tom*_*rik 3 ng2-bootstrap angular

嗨,在我的Angular 2应用程序中,我正在使用ng2-file-upload:

<input type="file" ng2FileSelect [uploader]="uploader" multiple (change)="onChange($event)">
Run Code Online (Sandbox Code Playgroud)

我想在我的javascript代码中从uploaderevent对象创建一个image对象的实例.不知何故喜欢:

public onChange(event) {
   let item = this.uploader.queue[0];
   let img = new Image();

   img.src = item.SOMETHING;
       OR
   img.src = event.SOMETHING;

}
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做.

Mr_*_*ect 6

你可以在没有任何第三方库的情况下做到这一点.

helloworld.component

import {Component} from 'angular2/core';

@Component({
  // Declare the tag name in index.html to where the component attaches
  selector: 'hello-world',

  // Location of the template for this component
  templateUrl: 'src/hello_world.html',
  styles: [`
    .preview img{
      max-height: 50px;
    }
  `]
})
export class HelloWorld {

  // Declaring the variable for binding with initial value
  yourName: string = '';

  fileChange(event) {
    let fileList: FileList = event.target.files;
    if(fileList.length > 0) {
      let file: File = fileList[0];
      var img = document.querySelector("#preview img");
      img.file = file;

      var reader = new FileReader();
      reader.onload = (function(aImg) { return function(e) { aImg.src = e.target.result; }; })(img);
      reader.readAsDataURL(file);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

SRC/hello_world.html

<div id="preview" class="preview">
  <img src="">
</div>
<form>
  <input type="file" (change)="fileChange($event)" placeholder="Upload file">
</form>
Run Code Online (Sandbox Code Playgroud)

工作的plunker