Spring optional download form with additional file

We create a profile page with a form that has an optional profile. We are using Spring 3.2

Here is the form: -

<form:form id="editMember" modelAttribute="memberAjaxEditModel" method="POST" class="form-horizontal" enctype="multipart/form-data" > ... <form:input path="fileData" type="file"/> ... </form> 

Here is the controller method: -

 @RequestMapping(value = "/{id}", method = RequestMethod.POST) public String onEditPost(@PathVariable long id, @Valid @ModelAttribute(MemberAjaxEditModel.KEY) MemberAjaxEditModel model, BindingResult result) throws ServiceRecoverableException { .... } 

Here is a model

 public class MemberAjaxEditModel { ... private CommonsMultipartFile fileData; ... } 

It works fine if the file is presented on the form, but there are errors in the BindingResult variable if the form is submitted without the file.

Here is the error: -

 Field error in object 'memberAjaxEditModel' on field 'fileData': rejected value []; codes [typeMismatch.memberAjaxEditModel.fileData,typeMismatch.fileData,typeMismatch.org.springframework.web.multipart.commons.CommonsMultipartFile,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [memberAjaxEditModel.fileData,fileData]; arguments []; default message [fileData]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'fileData'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile] for property 'fileData': no matching editors or conversion strategy found] 
+6
source share
5 answers

It turns out that it was a jQuery Form plugin that sent an empty string instead of the expected spring - nothing was sent.

I solved the problem using the before command before submitting to remove the fileData value if it was not populated like this: -

  function beforeSubmit(arr, $form, options){ var fileDataIndex = -1; $.each(arr, function(index, value) { if (value.name == "fileData"){ if (value.value.length == 0){ fileDataIndex = index; } } }); if (fileDataIndex != -1){ arr.remove(fileDataIndex); } } 

Hope this helps some googlers with the same issue.

+6
source

See also github issue 296 :

You can use the iframe option to force the recording of the same type for both cases:

iframe: true

+2
source

Try using StringMultipartFileEditor .

 @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringMultipartFileEditor()); } 
+2
source

Use org.springframework.web.multipart.MultipartFile instead of CommonsMultipartFile

+1
source

Do you have a multipartResolver bean defined in your application-context.xml ? If not, turn it on and try

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1000000"/> <!-- File size in bytes. --> </bean> 
0
source

All Articles