I am uploading image using input type file along with other user data. My model consists of the User class:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Rollno { get; set; }
public byte[] ProfileImage { get; set; }
public HttpPostedFile image { get; set; }
}
and a posted method in asp.net mvc:
public ActionResult AddUser(User user)
{
var files = Request.Files;
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
byte[] uploadFile = new byte[file.InputStream.Length];
}
return View();
}
I have an input tag like this:
<input id="imgInp" type="file" aria-label="Add photos to your post" class="upload" name="file" onchange="angular.element(this).scope().LoadFileData(this.files)" multiple="" accept="image/*">
and an angularJS controller like this:
angular.module('app', ['AngularDemo.BeerController']).
controller('formController',['$scope','$http', function ($scope,$http) {
alert('in controller');
var formData = new FormData();
$scope.LoadFileData = function (files) {
for (var file in files) {
formData.append("file", files[file]);
}
};
$scope.submit = function () {
alert('in submit');
$http.post("/Home/AddUser", formData, {
withCredentials: true,
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
}).success(function (response) {
});
}
}]);
My image file is posting back in the AddUser method but how do I send the model data? I have added ng-model="Name" etc in the form. How do I send $scope.Name etc with that image data since my $http.post taking form data as passing data?