I am trying to upload tiered files using FormData and spring.
HTML:
<input type="file" name="img" multiple id="upload-files">
JS Code:
var ajaxData = new FormData(); var files = $('#upload-files').prop('files'); for(var i=0;i<files.length;i++){ ajaxData.append('file['+i+']', files[i]); } ajaxData.append("file", files); $http.post('../rest/upload', ajaxData, { headers: {'Content-Type': undefined }, transformRequest: angular.identity });
Spring Controller Code:
@RequestMapping(value = "/upload", produces="application/json", method = RequestMethod.POST) @ResponseBody public String upload( @RequestParam ArrayList<MultipartFile> files ){ System.out.println(files.size()); return null; }
However, the number of files exits equal to 0 when sending a request with several files. When using array notation of MultipartFile[] files instead of ArrayList, it gives 400, Bad Request.
How to make spring controller work with multiple files? I can not find a solution to other SO issues.
java jquery spring ajax spring-mvc
Gautam kumar
source share