2013-02-08 36 views
6

हम एक ऐसे फॉर्म के साथ प्रोफाइल पेज बना रहे हैं जिस पर वैकल्पिक रूप से प्रोफाइल चित्र है। हम प्रपत्र स्प्रिंग 3.2वैकल्पिक फ़ाइल के साथ वसंत अपलोड फॉर्म वैकल्पिक

यहाँ का उपयोग कर रहे है: -:

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

यहाँ मॉडल

public class MemberAjaxEditModel { 

... 
private CommonsMultipartFile fileData; 
... 
} 

है यह ठीक काम करता है -

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

यहाँ नियंत्रक विधि है अगर फ़ाइल पर फ़ाइल सबमिट की गई है, लेकिन फ़ाइल के बिना फ़ॉर्म सबमिट किए जाने पर बाध्यकारी रीसेट चर में त्रुटियां हैं।

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

उम्मीद की कोशिश यह पता चला है कि यह jQuery Form plugin जो वसंत के एक खाली स्ट्रिंग भेज रही थी बजाय था - कुछ भी नहीं भेजे जाने के लिए।

मैं एक से पहले fileData मूल्य दूर करने के लिए प्रस्तुत करता है, तो यह बहुत तरह तैयार नहीं हुआ था का उपयोग कर समस्या हल: -

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); 
    } 
} 

मुझे आशा है कि यह एक ही समस्या के साथ कुछ Googlers मदद करता है।

+2

धन्यवाद @ एश, मुझे एक ही समस्या का सामना करना पड़ रहा था .. यह वास्तव में मेरी मदद करता था .. बस मैंने arr.splice (fileDataIndex, 1) का उपयोग किया; arr.remove (fileDataIndex) के बजाय; – Saurabh

+0

यह मुझे भी फॉर्म बनाता है लेकिन जब मैं सबमिट करने से पहले विकल्प में अपनी विधि का उपयोग करता हूं, तो वेब पेज को प्रतिक्रिया की सामग्री (मैं AJAX व्यवहार खो देता हूं) के साथ पुनः लोड किया जाता है ... – Labe

+0

मुझे वास्तव में पता नहीं है कि क्यों सौरभ संशोधन का उपयोग करके, मेरी समस्या चली गई है धन्यवाद – Labe

1

उपयोग org.springframework.web.multipart.MultipartFile बजाय CommonsMultipartFile

+0

क्या है जब आप() विधि अपने onEditPost से "@PathVariable लंबे आईडी" निकाल सकते हैं (और यह MemberAjaxEditModel में डाल दिया) क्या होता है? – Andre

0

आप multipartResolver सेम अपने application-context.xml में परिभाषित किया है करो -:

यहाँ त्रुटि है? यदि नहीं तो इस में शामिल हैं और

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

धन्यवाद, लेकिन मेरे पास पहले से ही यह था (एक @ कॉन्फिगुरेटन क्लास के रूप में)। यह पता चला कि यह अजाक्सफॉर्म प्लगइन था जो वसंत की अपेक्षाओं के बजाय एक खाली स्ट्रिंग भेज रहा था - भेजने के लिए कुछ भी नहीं। –

2

भी github issue 296 देखें: StringMultipartFileEditor उपयोग करने के लिए

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

iframe: true

+0

यह मेरे लिए काम किया! धन्यवाद! –

2

प्रयास करें।

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