<div class="container">
<form #form = "ngForm" (ngSubmit)="logForm(form.value)" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" ngModel>
</div>
<div class="form-group">
<label>File</label>
<input type="file" name="file" ngModel>
</div>
<button type="submit" class="btn btn-primary">SUBMIT</button>
</form>
</div>
<div class="container">
<form #form = "ngForm" (ngSubmit)="logForm(form.value)" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" ngModel>
</div>
<div class="form-group">
<label>File</label>
<input type="file" name="file" ngModel (change)="getFiles($event)">
</div>
<button type="submit" class="btn btn-primary">SUBMIT</button>
</form>
</div> |
import { Component } from '@angular/core';
@Component({
selector: 'ej-app',
templateUrl: 'src/home/home.component.html',
})
export class HomeComponent {
files : FileList;
getFiles(event){
this.files = event.target.files;
}
logForm(event) {
console.log(this.files);
}
}
|
import { Component } from '@angular/core';
import { Headers, Http, RequestOptions } from '@angular/http';
import { NgForm } from '@angular/forms';
@Component({
selector: 'ej-app',
templateUrl: './app.component.html',
})
export class AppComponent {
files: FileList;
filestring: string;
constructor(public http: Http) {
}
getFiles(event) {
this.files = event.target.files;
var reader = new FileReader();
reader.onload = this._handleReaderLoaded.bind(this);
reader.readAsBinaryString(this.files[0]);
}
_handleReaderLoaded(readerEvt) {
var binaryString = readerEvt.target.result;
this.filestring = btoa(binaryString); // Converting binary string data.
}
logForm(form: NgForm) {
console.log(this.files);
this.sendValues(form.name, form.password);
}
sendValues(name, password) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post('server-url', JSON.stringify({ Username: name, Password: password, FileData: this.filestring }), options). // http post method to sending data
subscribe(
(data) => {
console.log('Response received');
},
(err) => { console.log(err); },
() => console.log('Authentication Complete')
);
}
}
|
<div class="container">
<form #form = "ngForm" (ngSubmit)="logForm(form.value)" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" ngModel>
</div>
<div class="form-group">
<label>PassWord</label>
<input type="text" name="password" class="form-control" ngModel>
</div>
<div class="form-group">
<label>File</label>
<input type="file" name="myfile" ngModel (change)="getFiles($event)">
</div>
<button type="submit" class="btn btn-primary">SUBMIT</button>
</form>
</div> |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
[HttpPost, ValidateInput(false)]
public ActionResult SendUserData(UserModel user)
{
return Json(user);
}
}
public class UserModel
{
public string Username { get; set; }
public string Password { get; set; }
public String FileData { get; set; }
}
} |
Hi Bharamani,
Thanks for contacting Syncfusion support.
We couldn’t get input value from form.value, in the case of inputs with type file since the file object can be reached $event.target.files instead. Refer below link for issue detail.
NOTE: In DefaultValueAccessor the value used to update the bound element is $event.target.value.
So we suggest you to use change event to get input value for file type. Please refer to the below code snippet.
[app.component.html]
<div class="container"><form #form = "ngForm" (ngSubmit)="logForm(form.value)" enctype="multipart/form-data"><div class="form-group"><label>Name</label><input type="text" name="name" class="form-control" ngModel></div><div class="form-group"><label>File</label><input type="file" name="file" ngModel (change)="getFiles($event)"></div><button type="submit" class="btn btn-primary">SUBMIT</button></form></div>
[app.component.ts]
import { Component } from '@angular/core';@Component({selector: 'ej-app',templateUrl: 'src/home/home.component.html',})export class HomeComponent {files : FileList;getFiles(event){this.files = event.target.files;}logForm(event) {console.log(this.files);}}We have also implemented seed applications, to quick-start with Syncfusion JavaScript Angular 2 components. Refer to the below documentation link for getting started with seed application.We also implemented Angular 2 sample browser using our JavaScript Angular 2 components.
Please revert us back, if you need further assist on this.Regards,Abinaya S