Reputation: 31
Below is my code: jsp code
<form:form name="uploadForm" id="fileUploadForm" method="POST" enctype="multipart/form-data">
<input type="file" id="file" name="file" value="Import" ">
<input type = "hidden" name="strategy" />
<input type="button" value="Import" id="Import" />
</form:form>
Below jquery snippet binds the click function of the Import button
$("#Import").click(function(){
filePath = document.getElementById("file").value;
}
The filePath value is getting assigned in the above variable filePath Below piece of code makes an Ajax call to the controller
var formData = new FormData($('#fileUploadForm')[0]);
formData.append('tax_file', $('input[type=file]')[0].files[0]);
$.ajax({
url: 'ajax/getuploadFile.html',
data: {
formData:formData, strategy:strategy,
},
type: "POST",
contentType: false,
processData: false,
success : function(response){
alert(response);
},
error: function() {
alert("Inside");
}
});
java controller class
@RequestMapping(value = "ajax/getuploadFile.html", method = RequestMethod.POST)
public @ResponseBody String getuploadFile(HttpServletRequest request,
@RequestParam("formData") MultipartFile attachment,
@RequestParam("strategy") String strategy,
) throws Exception {
Can anyone please help me.. I am facing the exception that current request is not multipart request. We are trying to import .csv file through import functionality mentioned in jsp code.
Upvotes: 1
Views: 1229
Reputation: 141
Please try this way:
Java controller
@Controller
public class TestController {
@RequestMapping("/test")
@ResponseBody
public String upload(
@RequestPart("file") MultipartFile multipartFile,
@RequestParam("strategy") String strategy) {
return "ok";
}
@RequestMapping
public ModelAndView index() {
return new ModelAndView("/WEB-INF/jsp/upload.jsp");
}
}
JSP file
<html>
<body>
<form name="uploadForm" id="fileUploadForm" method="POST"enctype="multipart/form-data">
<input type="file" id="file" name="file" value="Import"">
<input type="hidden" name="strategy" />
<input type="submit" value="Import" id="Import" />
</form>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$('form[name="uploadForm"]').submit(function(event){
event.preventDefault();
var formData = new FormData($('#fileUploadForm')[0]);
formData.append('file', $('input[type=file]')[0].files[0]);
$.ajax({
url: '/test',
data: formData,
type: "POST",
contentType: false,
processData: false,
success : function(response){
alert(response);
},
error: function() {
alert("Inside");
}
});
return false;
});
</script>
</body>
</html>
I tried to test it. It's working well.
Upvotes: 1
Reputation: 141
"formData" already contain "file" and "strategy", so you just need: data: formData
var formData = new FormData($('#fileUploadForm')[0]);
formData.append('tax_file', $('input[type=file]')[0].files[0]);
$.ajax({
url: 'ajax/getuploadFile.html',
data: formData,
type: "POST",
contentType: false,
processData: false,
success : function(response){
alert(response);
},
error: function() {
alert("Inside");
}
Upvotes: 0