I am new to Angular JS but I have good experience in Spring MVC. So, now I have the requirement where I need to upload a file. But, here I need to develop UI screen for uploading excel sheet using AngulaJS and backend will be Spring controller. Can anyone provide example for the same.
1 Answer
In Spring, this would be the method to accept multipart file uploads. myFile will contain all information of the uploaded file you need.
@RequestMapping(value = "{userId}/profile/image", method = RequestMethod.POST)
public ResponseEntity<String> uploadProfileImage(@PathVariable String userId, @RequestParam("file") MultipartFile myFile) {
...
}
You might want to add the following Spring Bean to your setup, as well:
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setResolveLazily(false);
resolver.setDefaultEncoding("utf-8");
return resolver;
}
In Angular, there are several projects that allow for file uploads.
Angular1: https://github.com/nervgh/angular-file-upload
Angular2: https://github.com/valor-software/ng2-file-upload/tree/master/demo
Both easy to use with demos and samples on their github page.
1 Comment
Jan B.
In some cases you might have specific requirements for the resolution of multipart requests. For instance, we had some issues with encodings (default is ISO-8859-1 according to the servlet spec, not utf8).