Reputation: 568
When I tried to upload a csv file to spring controller by Ajax I am gettig the error HTTP Status 400 - Required request part 'file' is not present
My ajax code is
<script type="text/javascript">
var form = $('#correctAnswerData');
form.on('submit', function(e) {
e.preventDefault();
var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
console.log("form data " + formData);
$.ajax({
url: 'answerdatacheck',
data: formData,
processData: false,
contentType: false,
type: 'PUT',
success: function(data) {
alert(data);
},
error: function() {
$('#errorMsg').html("An error occurred.");
}
});
}
);
</script>
and my controller code is
@RequestMapping(value = "/answerdatacheck", method = RequestMethod.PUT)
public String regiscorrectAnswerData(Model model, @RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
model.addAttribute("alertStatus", 1);
model.addAttribute("alertMessage", "File is empty");
return "jsonView";
}
model.addAttribute("alertStatus", 2);
return "jsonView";
}
What will be the reason actually I have included the bean in application-context.xml
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
</bean>
am I missing something.
Upvotes: 0
Views: 939
Reputation: 568
It was just the error of accepting the file I just changed
formData.append('file', $('input[type=file]')[0].files[0]);
to
formData.append('file', $('#file1')[0].files[0]);
and it got fixed
Upvotes: 2