Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to upload multiple files using angular js, for this I am using FormData() .

Here is my form fields

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

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

    <input name="Id" type="text" ng-model="user.Id" />
<input name="Name" type="text" ng-model="user.Name" />

Here is my Asp.Net MVC controller

public ActionResult AddUser(string user, HttpPostedFileBase[] files)
{
    // parse user into User
}

and the angular controller

.controller('formController',['$scope','$http', function ($scope, $http) {
    $scope.user = {
        Id: 0,
        Name: ""
    };
    $scope.files = [];

    $scope.LoadFileData = function(files) {
        $scope.files = files;
    };

    $scope.submit = function() {
        $http({
            url: "/Home/AddUser",
            method: "POST",
            headers: { "Content-Type": undefined },
            transformRequest: function(data) {
                var formData = new FormData();
                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, files: $scope.files }
        })
        .success(function(response) {   });
    };
});

Problem is that I want to upload more than one image one for profile and the other for avatar . When I upload both only the second one shows up which override the previous one . I tried to push $scope.files.push(files) but it gives null.Need help what I am missing there.

share|improve this question
    
why do you have name="file" on input fields remove that – ConvertToInt32 Aug 22 '15 at 21:05
    
I removed that also – Ghazanfar Khan Aug 22 '15 at 21:11
up vote 1 down vote accepted

Change the LoadFileData function

$scope.LoadFileData = function (files) {
        $scope.files.push(files[0]);
    };

files returns a Filelist object and actual file object is at index 0.

share|improve this answer
    
Bro how to identity the image for profile and avatar this gives an array of two images? – Ghazanfar Khan Aug 22 '15 at 22:06
    
instead of files array use two different names when sending formData – ConvertToInt32 Aug 22 '15 at 22:07
    
Can u edit that with answer? – Ghazanfar Khan Aug 22 '15 at 22:08
    
This not work avatarImage: $scope.files[0],profileImage:$scope.files[1] – Ghazanfar Khan Aug 22 '15 at 22:09
    
How to use two different names when sending formData? – Ghazanfar Khan Aug 22 '15 at 22:17
$scope.LoadFileData = function (element) {
        var filesX = element.files;
        for(var i=0;i<filesX.length;i++)
        {
          $scope.files.push(filesX[i]); 
        }
    }
share|improve this answer

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.