Java Controller class:
@RequestMapping(value = \"/upload\" , method = RequestMethod.POST , consumes=\"multipart/form-data\")
public void upload(@RequestParam Mul
Try the following. It is working fine for me.
HTML You should have
<input id="file-0a" class="file" type="file" file-model="myFile" name="myFile" />
<button ng-click="uploadFile()">upload me</button>
Note the name of the input.
Then in JS controller method you should have
$scope.uploadFile = function(){
var formData=new FormData();
formData.append("file",myFile.files[0]); //myFile.files[0] will take the file and append in formData since the name is myFile.
$http({
method: 'POST',
url: '/upload', // The URL to Post.
headers: {'Content-Type': undefined}, // Set the Content-Type to undefined always.
data: formData,
transformRequest: function(data, headersGetterFunction) {
return data;
}
}).success(function(data, status) {
})
.error(function(data, status) {
});
}
Now in Your Java Controller class
@RequestMapping(value = "/upload" , method = RequestMethod.POST)
public void upload(HttpServletRequest request) {
//org.springframework.web.multipart.MultipartHttpServletRequest
MultipartHttpServletRequest mRequest;
mRequest = (MultipartHttpServletRequest) request;
Iterator<String> itr = mRequest.getFileNames();
while (itr.hasNext()) {
//org.springframework.web.multipart.MultipartFile
MultipartFile mFile = mRequest.getFile(itr.next());
String fileName = mFile.getOriginalFilename();
System.out.println("*****"+ fileName);
//To copy the file to a specific location in machine.
File file = new File('path/to/new/location');
FileCopyUtils.copy(mFile.getBytes(), file); //This will copy the file to the specific location.
}
}
Hope this works for You. And do Exception Handling also.