2013-02-08 56 views
6

Chúng tôi đang tạo trang tiểu sử có biểu mẫu tùy chọn có ảnh hồ sơ trên đó. Chúng tôi đang sử dụng Spring 3.2Biểu mẫu tải lên mùa xuân tùy chọn với tệp tùy chọn

Dưới đây là hình thức: -

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

Dưới đây là phương pháp điều khiển: -

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

Đây là Model

public class MemberAjaxEditModel { 

... 
private CommonsMultipartFile fileData; 
... 
} 

Nó hoạt động tốt nếu tệp được gửi trên biểu mẫu, nhưng có lỗi trong biến BindingResult nếu biểu mẫu được gửi mà không có tệp.

Dưới đây là các lỗi: -

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] 

Trả lời

6

Hóa ra đó là jQuery Form plugin được gửi một chuỗi rỗng thay vì những gì mùa xuân hy vọng - không có gì được gửi đi.

tôi giải quyết vấn đề bằng cách sử dụng trước khi nộp để loại bỏ các giá trị fileData nếu nó không được dân cư như vậy: -

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

Tôi hy vọng điều này sẽ giúp một số nhân viên của Google với cùng một vấn đề.

+2

Cảm ơn @ Ash, tôi đã phải đối mặt với cùng một vấn đề .. nó thực sự đã giúp tôi .. Chỉ cần tôi đã sử dụng arr.splice (fileDataIndex, 1); thay vì arr.remove (fileDataIndex); – Saurabh

+0

Nó hoạt động hình thức tôi quá nhưng khi tôi sử dụng phương pháp của bạn trong tùy chọn beforeSubmit, trang web được tải lại với nội dung của phản ứng (tôi bị mất hành vi AJAX) ... – Labe

+0

Tôi không thực sự biết tại sao nhưng bằng cách sử dụng sửa đổi Saurabh, vấn đề của tôi đã biến mất. Cảm ơn – Labe

1

Sử dụng org.springframework.web.multipart.MultipartFile thay vì CommonsMultipartFile

+0

Điều gì xảy ra khi bạn xóa "@PathVariable long id" khỏi phương thức onEditPost() của bạn (và đặt nó vào MemberAjaxEditModel)? – Andre

0

Bạn đã đậu multipartResolver quy định tại application-context.xml của bạn? Nếu không sau đó bao gồm này và cố gắng

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

Cảm ơn, nhưng tôi đã có điều này (như một lớp học @Configuraton). Hóa ra đó là plugin AjaxForm đang gửi một chuỗi rỗng thay vì những gì mùa xuân mong đợi - không có gì được gửi đi. –

2

Xem thêm github issue 296:

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

iframe: true

+0

Điều này phù hợp với tôi! Cảm ơn! –

2

Cố gắng sử dụng StringMultipartFileEditor.

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    binder.registerCustomEditor(String.class, new StringMultipartFileEditor()); 
} 
Các vấn đề liên quan