4

MY html file: Upload.html

<tr ng-repeat="expenses in finalJson.comments">
 <td >
   <div class="image-upload">
     <label for="file-input"> 
      <img src="../images/upload1.jpg" style="width: 20px;"/>{{data.files[0].name}} 
    </label>
     <input id="file-input" type="file" ng-model="expenses.files"  ngf-select accept="*" value=""/>
    </div>
 </td>
</tr>

controller : UploadController I have used this to get the file names after the upload is clicked

var json = JSON.stringify($scope.finalJson.comments); console.log(json);

Screenshot:

enter image description here

When I upload one file its working file, when am adding more then one file I cant get the second file name in the console. Can any one suggest me, how to get many files at once after the upload click was made.

[{
    "index": 1,
    "amount": "10",
    "$$hashKey": "object:5",
    "date": "2016-04-11",
    "Category": "58",
    "note": "wdxw",
    "paid_to": "swdw",
    "files": {
        "webkitRelativePath": "",
        "lastModified": 1450934331000,
        "lastModifiedDate": "2015-12-24T05:18:51.000Z",
        "name": "node-js.pdf",
        "type": "application/pdf",
        "size": 182649
    }
}, {
    "$$hashKey": "object:31",
    "date": "2016-04-05",
    "Category": "60",
    "note": "scds",
    "paid_to": "dsad",
    "amount": "20"
}]
5
  • While I upload two files I got one file name in console Commented Apr 7, 2016 at 7:21
  • @smile refer this for multiple file upload , its working fine plnkr.co/edit/oVpgrSWQAcFdV26aVKv7?p=preview Commented Apr 7, 2016 at 7:33
  • @siva I need inside the ng-repeat code Commented Apr 7, 2016 at 7:56
  • @smile refer this it's working as you expected jsfiddle.net/JeJenny/vtqavfhf Commented Apr 7, 2016 at 7:59
  • @siva in single button I upload all files I dont want upload part now In Json console I need, two upload file names Commented Apr 7, 2016 at 8:02

1 Answer 1

1

Use this code in ng-repeat

Html code :

<div ng-controller = "myCtrl">
    <div ng-repeat="fileInput in fileInputs">
        <input type="file" file-model="{{'myFile' + $index}}"/>
        <button ng-click="uploadFile('myFile' + $index)">upload me</button>
    </div>
</div>

Controller code :

var myApp = angular.module('myApp', []);

myApp.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model, modelSetter;

            attrs.$observe('fileModel', function(fileModel){
                model = $parse(attrs.fileModel);
                modelSetter = model.assign;
            });

            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope.$parent, element[0].files[0]);
                });
            });
        }
    };
}]);

myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(){
        })
        .error(function(){
        });
    }
}]);

myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
    $scope.fileInputs = [1,2,3];
    $scope.uploadFile = function(filename){
        var file = $scope[filename];
        console.log('file is ' + JSON.stringify(file));
        console.dir(file);
        var uploadUrl = "http://httpbin.org/post";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    };

}]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.