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

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -