asp.net MVC 3 앱을 업로드 한 파일이 포함 된 클래스에 바인딩하는 데 문제가 있습니다.asp.net mvc에서 파일 업로드를 처리하는 가장 좋은 방법은 무엇입니까?
여기 내 클래스의 :
public class PhotoAndCaption : IValidatableObject
{
[Required]
public HttpPostedFile ImageFile { get; set; }
[Required]
public string Caption { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
//make sure ImageFile is jpg image of proper size.
yield return new ValidationResult("This shit ain't right!!!!");
}
}
여기에 뷰의 :
@using(Html.BeginForm("UploadImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<p><input type="file" id="ImageFile" name="ImageFile" /></p>
<p>@Html.TextBoxFor(m => m.Caption)</p>
<p></p>
<input type="submit" value="Upload File" />
}
무엇 발생하면 캡션이 제대로 결합하지만 ImageFile하지 않는 것입니다. IValidatableObject를 구현 한 클래스에 바인딩하고 싶습니다. 그래서 업로드 된 이미지의 옳은 픽셀 크기 등을 깔끔하게 확인할 수 있습니다. 그렇지 않은 경우 의미있는 오류 메시지로 응답하고 싶습니다. 이상적으로는 한 곳에서 모든 클래스 유효성 검사를하고 싶습니다. 캡션과 나중에 한 곳에서 추가 할 수있는 다른 필드를 확인할 수 있습니다.
이 캡션 속성은 올바르게 바인딩되지만 ImageFile은 바인딩되지 않습니다.
참으로 해결책 인 경우 직접 답변을 수락 할 수 있습니다. –