Sharing Top Content from the Angular-sphere.

File Upload using Angularjs and Spring-MVC Restful service

  • 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.
  • 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 a code for that

    Here I am storing that EXCEL FILE in “C:/Data/DemoUpload/” with an modified filename(upload.xlsx).

  • This will copy the file that was created and put them into the place that we specify.
  • Change the “” place with your server url and run the code to upload an excel sheet to your backend.

A simple file upload using angularjs, spring and restful webservice. Many have a thought about how to do this using spring MVC and here is the answer.

@JavaScriptKicks: File Upload Using AngularJS and Spring by @Vishal_Srini #javascript #angularjs via @JavaScriptKicks

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.

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).

LETS BEGIN:

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 – https://mvnrepository.com/artifact/commons-io/commons-io 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) @Produces(MediaType.APPLICATION_JSON) public Data continueFileUpload(HttpServletRequest request, HttpServletResponse response){ MultipartHttpServletRequest mRequest; String filename = “upload.xlsx”; try { mRequest = (MultipartHttpServletRequest) request; mRequest.getParameterMap(); Iterator itr = mRequest.getFileNames(); while (itr.hasNext()) { MultipartFile mFile = mRequest.getFile(itr.next()); String fileName = mFile.getOriginalFilename(); System.out.println(fileName); java.nio.file.Path path = Paths.get(“C:/Data/DemoUpload/” + filename); Files.deleteIfExists(path); InputStream in = mFile.getInputStream(); Files.copy(in, path); } } catch (Exception e) { e.printStackTrace(); } 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.

 

× Errors! {{errors.value}}

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.

File Upload using Angularjs and Spring-MVC Restful service

Comments are closed, but trackbacks and pingbacks are open.