Created
June 16, 2016 01:46
-
-
Save abdulrafique/9219f7164fdf5dc6dfa8da110be6a04e to your computer and use it in GitHub Desktop.
Sample Code for file upload using Spring and AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myApp = angular.module("myApp",[]); | |
myApp.controller("AppController",['$scope','$http', 'fileUpload', function($scope, $http, fileUpload){ | |
$scope.uploadFile = function(){ | |
var file = $scope.myFile; | |
console.log('file is ' ); | |
console.dir(file); | |
var uploadUrl = "/myTest/uploadFile"; | |
fileUpload.uploadFileToUrl(file, uploadUrl); | |
$scope.filename=file.name; | |
}; | |
}]); | |
myApp.directive('fileModel', ['$parse', function ($parse) { | |
return { | |
restrict: 'A', | |
link: function(scope, element, attrs) { | |
var model = $parse(attrs.fileModel); | |
var modelSetter = model.assign; | |
element.bind('change', function(){ | |
scope.$apply(function(){ | |
modelSetter(scope, element[0].files[0]); | |
}); | |
}); | |
} | |
}; | |
}]); | |
myApp.service('fileUpload', ['$http', function ($http) { | |
this.uploadFileToUrl = function(file, uploadUrl){ | |
var fd = new FormData(); | |
fd.append('file', file); | |
result= $http.post(uploadUrl, fd, { | |
transformRequest: angular.identity, | |
headers: {'Content-Type': undefined} | |
}) | |
.success(function(){ | |
}) | |
.error(function(){ | |
}); | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment