How can I get the reference of a file through the HTML's input tag? (Angular 2) -


i want upload image n firebase. have file first. how can image of computer, instance, through html? i'm trying this, don't know what's return of this. me guys...

<input type="file" name="image" [(ngmodel)]="myimage"> 

you can reference dom element using elementref , extract data of file uploaded element reference. use #variablename reference file input.

<input #fileinput (change)="fileupload()" type="file" name="pic" accept="image/*"> 

in component class, reference file input using viewchild.

import {component, elementref, renderer2, viewchild} '@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 uploaded file through elementref     this.uploadedfile = this.el.nativeelement.files[0];     } } 

i have created sample plunker here.


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -