javascript - How to add multiple files into a array and access files one by one in AngularJs -
i trying add multiple files file array , later on access each files in angularjs.so far have created custom directive read files looking @ posts.it working fine single file.but requirement want upload multiple files , add them array , later on access created file array when accessing data.i newbie angularjs please me achieve this.
html code snippet
<div class="input input-file "> <span class="button"><input on-read-file="readfilecontent($filecontent)" type="file" id="attachments" name="file" data-ng-model="attachments" onchange="this.parentnode.nextsibling.value = this.value">browse</span> <input type="text" placeholder="attach files" readonly=""> </div>
custom directive read input files
var mimetype,filename; testmodule.directive('onreadfile', function ($parse) { return { restrict: 'a', scope: false, link: function(scope, element, attrs) { var fn = $parse(attrs.onreadfile); element.on('change', function(onchangeevent) { var reader = new filereader(); reader.onload = function(onloadevent) { scope.$apply(function() { fn(scope, {$filecontent:onloadevent.target.result}); }); }; reader.readastext((onchangeevent.srcelement || onchangeevent.target).files[0]); //get uploaded file mime type mimetype=(onchangeevent.srcelement || onchangeevent.target).files[0].type; filename=(onchangeevent.srcelement || onchangeevent.target).files[0].name; }); } }; });
read file content method
$scope.readfilecontent = function($filecontent){ $scope.content = $filecontent; };
i suggest check post on stackoverflow:
upload multiple files in angular
one of answers contains working example:
http://plnkr.co/edit/b13t84j5ipzinmh1f862?p=preview
it uses directive named ng-file-model:
<input type="file" ng-file-model="files" multiple />
Comments
Post a Comment