You can reference the DOM Element using ElementRef and then extract the data of the file uploaded from the element reference. Use #variableName to reference the file input.
<input #fileInput (change)="fileUpload()" type="file" name="pic" accept="image/*">
In your component class, reference the file input using ViewChild.
import {Component, ElementRef, Renderer2, ViewChild} from '@angular/core'
@Component({
selector: 'my-app',
template: `
<div>
<h2>{{name}}</h2>
<input #fileInput (change)="fileUpload()" type="file" name="pic" accept="image/*">
</div>
`,
})
export class App {
name:string;
@ViewChild('fileInput') el:ElementRef;
constructor(private rd: Renderer2) {
this.name = `Angular : File Upload`
}
fileUpload() {
console.log(this.el);
// Access the uploaded file through the ElementRef
this.uploadedFile = this.el.nativeElement.files[0];
}
}
I have created a sample plunker here.