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]
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
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
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