angularjs - Upload Image file with other data using Asp.net Web api 2? -
i build web application using angularjs , asp.net web api.
in view of register user have , input file upload user image , other personal information such phone number , full name , want send informations in 1 object web api.
1. web api action register :
public httpresponsemessage register(registerbindingmodel model) { try { //we test if data correct if (!modelstate.isvalid) { return request.createerrorresponse(httpstatuscode.badrequest, modelstate); }
2 . registerbindingmodel :
public class registerbindingmodel { public string fullname {get;set;} public string phonenumber {get;set;} public httppostedfilebase file { get; set; } }
3 . angularjs view :
<form role="form" name="addform" enctype="multipart/form-data" novalidate> <!--personal informations--> <div class="row"> <div class="col-xs-12 text-center" > <div class="form-group"> <div class="btn btn-default btn-file"> <i class="fa fa-plus"></i> select logo <input type="file" name="file" ng-model="vm.registration.file" /> </div> <input type="text" name="fullname" ng-model="vm.registration.fullname" /> <input type="text" name="phonenumber" ng-model="vm.registration.phonenumber" /> <div> <button ng-click="vm.register()">save</button> </div> </div>
4. angular controller :
vm.register = function () { authservice.saveregistration(vm.registration).then( function (results) { vm.cancel(); }, function (error) { }); };
5.angular authservice :
var _saveregistration = function (registration) { return $http .post(servicebase + 'api/account/register', registration) .then(function (response) { return response; }); };
even if select file in web api action register have model.file equal null , other data fullname , phonenumber have correct value !
can me please ?
thank you
Comments
Post a Comment