2013-02-08 35 views
6

我們正在創建一個配置文件頁面,其中可能有一個配置文件圖片。我們使用Spring 3.2彈簧上傳表格可選帶有可選文件

這裏是形式: -

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

這裏是控制器方法: -

@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; 
... 
} 

它工作正常如果在表單上提交了一個文件,但是如果表單沒有提交文件,那麼BindingResult變量就會出現錯誤。

以下是錯誤: -

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

我希望這有助於一些Google員工同樣的問題。

+2

謝謝@Ash,我面臨着同樣的問題..它真的幫助我..只是,我用arr.splice(fileDataIndex,1);而不是arr.remove(fileDataIndex); – Saurabh 2013-06-27 11:12:47

+0

它工作形式我也是,但當我在選項beforeSubmit中使用你的方法,網頁重新加載響應的內容(我失去了AJAX行爲)... – Labe 2015-05-18 14:59:07

+0

我真的不知道爲什麼,但通過使用Saurabh修改,我的問題沒有了。謝謝 – Labe 2015-05-18 15:07:48

1

使用org.springframework.web.multipart.MultipartFile而不是CommonsMultipartFile

+0

從onEditPost()方法(並將其放入MemberAjaxEditModel)中刪除「@PathVariable long id」會發生什麼? – Andre 2013-02-08 11:59:55

0

你已經在你的application-context.xml定義的multipartResolver豆?如果沒有,那麼包括此和嘗試

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

謝謝,但我已經有了這個(作爲@Configuraton類)。事實證明,這是AjaxForm插件發送一個空字符串,而不是春天期望的 - 什麼都不會發送。 – 2013-02-11 14:24:48

2

github issue 296參見:

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

iframe: true

+0

這對我有效!謝謝! – 2017-09-07 19:27:31

2

嘗試使用StringMultipartFileEditor

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