Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have two input type file controls in my view page . One is for Avatar and the other is for profile image. And I am getting both images in code behind. Here is my AngularJS controller

    var app = angular.module('ProfileModule', []);
app.controller('ProfileController',['$scope','$http', function ($scope,$http) {
    $scope.user = {};
    $scope.files = [];
    var formData = new FormData();
    $scope.LoadFileData = function (files) {
        for (var file in files) {
            formData.append("file", files[file]);
        }
    };
    $scope.submit = function () {
        $http({
            url: "/Profiles/Edit",
            method: "POST",
            headers: { "Content-Type": undefined },
            transformRequest: function (data) {
                formData.append("user", angular.toJson(data.user));
                //for (var i = 0; i < data.files.length; i++) {
                //    formData.append("files[" + i + "]", data.files[i]);
                //}
                return formData;
            },
            data: { user: $scope.user }
        })
        .success(function (response) { });
    }
}]);

Here are the two controls

<input id="selector" type="file"   onchange="angular.element(this).scope().LoadFileData(this.files)" accept="image/*">



<input id="selector" type="file"   onchange="angular.element(this).scope().LoadFileData(this.files)" accept="image/*">

Here is my MVC controller

public ActionResult Edit(string user, HttpPostedFileBase[] files)
        {}

I am getting rest model value in string and later parsing that to model but images file showing null in files so I am getting them in Request.Files . What edit should I do to get that images file in the array.

share|improve this question
    
Your input elements should have unique ids, just like any other elements should. – Mihai Caracostea Aug 22 '15 at 19:41

You can just tweak your controller action to accept an IEnumerable of HttpPostedFileBase instances:

public ActionResult Index(IEnumerable<HttpPostedFileBase> files)

If you want to upload several file fields each with a different name, for the model binder to bind them correctly your controller action signature should look like this:

public ActionResult Create(HttpPostedFileBase avatar, 
                           HttpPostedFileBase profile)

More info

share|improve this answer
    
Suppose if user input only one image how would I know to place that in avatar or profile? – Ghazanfar Aug 22 '15 at 19:14
    
@Ghazanfar Check the update. – Sirwan Afifi Aug 22 '15 at 19:21
    
see edit I paste the whole code – Ghazanfar Aug 22 '15 at 19:28

Both your input type file controls should have name attributes set to something like "profileImage" and "avatarImage". Then define your controller action like so:

[HttpPost]
ActionResult Edit (string user, HttpPostedFileBase profileImage, HttpPostedFileBase avatarImage)
{
...
}

And html:

<input id="selector" name="profileImage" type="file" onchange="angular.element(this).scope().LoadFileData(this.files)" accept="image/*"> 

<input id="selector" name="avatarImage" type="file" onchange="angular.element(this).scope().LoadFileData(this.files)" accept="image/*">

EDIT: Check if the parameters are non-null and you are ready to go.

EDIT: added user parameter to controller action.

EDIT: added html code.

share|improve this answer
    
see edit I past the whole code – Ghazanfar Aug 22 '15 at 19:28
    
@Ghazanfar See edits, please. – Mihai Caracostea Aug 22 '15 at 19:40
    
Both HttpPostedFileBase shows null – Ghazanfar Aug 22 '15 at 19:46
    
Have you also added the name attributes to your inputs? – Mihai Caracostea Aug 22 '15 at 19:47
    
Yes I have added that there must be a problem in transform request and data in http post . – Ghazanfar Aug 22 '15 at 19:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.