File Upload using Angularjs and Spring-MVC Restful service

Reading Time: 6 minutes

Hi everyone,
For the past few days many started asking me like how to create a SPRING-MVC file upload using ANGULARJS. So I thought of creating a post for it where I create a working code that fetches a MULTIPART file from FRONTEND and get it processed in backend.

To begin this you need to know basics of ANGULARJS and RESTFUL WEBSERVICES.

Web hosting

The project is going to be created using MAVEN BUILD TOOL. So you may need to know the basics of MAVEN too. If not then you dont need to worry. Install the jar’s externally and run it with the build tool you like (Example ANT etc).


Here is dependencies in POM.XML that is needed for our code. (If not using MAVEN then download “Apache common” JARs externally. Here is an easy way to do it. Go to this URL – and download the commons-io dependency jar. Inside that you can search the artifactId mentioned in the POM.xml and can fetch the required JAR from that site)



Now will create a bean to add the maximum file upload limit. Here is the code for that. Place this in SPRING configuration XML. Most commonly named as SPRING-SERVLET.XML

Now lets create a controller for fetching the file from frontend and to process that file and storing it in a specified path. Here is a code for that

@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public Data continueFileUpload(HttpServletRequest request, HttpServletResponse response){
        MultipartHttpServletRequest mRequest;
	String filename = "upload.xlsx";
	try {
	   mRequest = (MultipartHttpServletRequest) request;

	   Iterator itr = mRequest.getFileNames();
	   while (itr.hasNext()) {
	        MultipartFile mFile = mRequest.getFile(;
	        String fileName = mFile.getOriginalFilename();
	        java.nio.file.Path path = Paths.get("C:/Data/DemoUpload/" + filename);
	        InputStream in = mFile.getInputStream();
	        Files.copy(in, path);
	   } catch (Exception e) {
	return null;

Here I am storing that EXCEL FILE in “C:/Data/DemoUpload/” with an modified filename(upload.xlsx). This uses two items one is fetching the file as a map using MultipartHttpServletRequest, and saving that file using Input stream buffer. This will copy the file that was created and put them into the place that we specify.

Now lets create a front end for clients. Here is a simple script for it.

 <div class="jumbotron"><input type="file" /> <button>upload me</button></div>
 <div class="alert alert-danger fade in"><a class="close" href="#" data-dismiss="alert">×</a> <strong>Errors!</strong> {{errors.value}}</div>
 var myApp = angular.module('myApp', []);

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(){
 modelSetter(scope, element[0].files[0]);

myApp.service('fileUpload', ['$q','$http', function ($q,$http) {
 var deffered = $q.defer();
 var responseData;
 this.uploadFileToUrl = function(file, uploadUrl){
 var fd = new FormData();
 fd.append('file', file);
 return $, fd, {
 transformRequest: angular.identity,
 headers: { 'Content-Type' : undefined}

/* $scope.errors =; */
 responseData = response;
 return deffered.promise;
 return deffered.promise;


this.getResponse = function() {
 return responseData;


myApp.controller('myCtrl', ['$scope', '$q', 'fileUpload', function($scope, $q, fileUpload){
 $scope.dataUpload = true;
 $scope.errVisibility = false;
 $scope.uploadFile = function(){
 var file = $scope.myFile;
 console.log('file is ' );

var uploadUrl = "<give-your-url>/continueFileUpload";
 fileUpload.uploadFileToUrl(file, uploadUrl).then(function(result){
 $scope.errors = fileUpload.getResponse();
 $scope.errVisibility = true;
 }, function(error) {


Here we created a directive and a service for fetching the data and putting it in backend. We used $q for asynchronus calls. This is called as PROMISE in angularjs. In our next course I will surely describe like how to create promises and what is asynchronus calls. Change the “” place with your server url and run the code to upload an excel sheet to your backend.


Love to code and Love to learn. A passionate technology lover who likes to sit with laptop even for weeks :D

You may also like...

12 Responses

  1. 97Bella says:

    I must say you have hi quality content here. Your blog can go viral.
    You need initial boost only. How to get it? Search for:
    Miftolo’s tools go viral

  2. Manju Parkavi says:

    That is easy to learn File Upload from this site. Very helpful at the needed time. Thanks 🙂

  3. oanhpv says:

    Can you give me full project…?


  4. oanhpv says:

    Can you give me full project…?
    Thank so much.

  5. oanhpv says:

    can you see how to config to upload file as you use?


    • vichu says:

      The above mentioned items will work. If not try adding a bean in config file that sets the maximum size.


      <beans:bean id="multipartResolver"
      	<!-- setting maximum upload size -->
      	  <beans:property name="maxUploadSize"
                    value="100000" />

      Send your project to or put in any repository and give me control so that I can check for errors. 🙂

  6. ankit says:

    which angularjs version ur using??

  7. ashoka says:

    Hi vichu

    this one really good example,but.
    Can you please give me same example in angular2.0 ,springmvc source code.

    this is my mail id

  8. Sheetal says:

    Hi Vichu,

    I need to implement attaching of documents using AngularJS and REST(similar as above) and then to salesforce(need some understanding on this). Will you be able to help?

  9. vishal says:

    hi i need to upload an excel sheet and store it in db ..need some help on that

  10. Rishabh says:

    please send me the full code at

Leave a Reply

Your email address will not be published. Required fields are marked *