in simple words
in Html - add below code only
<form name="upload" class="form" data-ng-submit="addFile()"> <input type="file" name="file" multiple onchange="angular.element(this).scope().uploadedFile(this)" /> <button type="submit">Upload </button> </form>
in the controller - This function is called when you click "upload file button". it will upload the file. you can console it.
$scope.uploadedFile = function(element) { $scope.$apply(function($scope) { $scope.files = element.files; }); }
add more in controllers - below code add into the function . This function is called when you click on button which is used "hitting the api (POST)". it will send file(which uploaded) and form-data to the backend .
var url = httpURL + "/reporttojson" var files=$scope.files; for ( var i = 0; i < files.length; i++) { var fd = new FormData(); angular.forEach(files,function(file){ fd.append('file',file); }); var data ={ msg : message, sub : sub, sendMail: sendMail, selectUsersAcknowledge:false }; fd.append("data", JSON.stringify(data)); $http.post(url, fd, { withCredentials : false, headers : { 'Content-Type' : undefined }, transformRequest : angular.identity }).success(function(data) { toastr.success("Notification sent successfully","",{timeOut: 2000}); $scope.removereport() $timeout(function() { location.reload(); }, 1000); }).error(function(data) { toastr.success("Error in Sending Notification","",{timeOut: 2000}); $scope.removereport() }); }
in this case .. i added below code as form data
var data ={ msg : message, sub : sub, sendMail: sendMail, selectUsersAcknowledge:false };
FormData
andangular-file-model
. To understand how to usefile-model
, see embed.plnkr.co/plunk/0ZHCsR. It worked for me.