2011-01-11 5 views
3

안녕하세요 ... 내보기에 업로드 컨트롤이 있습니다. 이 컨트롤을 모델 데이터 (LabelFor 또는 TextBoxFor와 같은 것)와 연결하는 방법이 있습니까? 페이지로드시 파일 업로드 제어에서 내 정보가 손실되므로 ThxMVC - 파일 업로드

답변

0

예, 속성 유형에 HttpPostedFileBase 클래스를 사용하면 다른 속성과 마찬가지로 바인딩됩니다.

+0

[HttpPost] public ActionResult Action(ViewModel model) { if (ModelState.IsValid) { // Use your file here using (MemoryStream memoryStream = new MemoryStream()) { model.File.InputStream.CopyTo(memoryStream); } } } 
? (http://www.w3.org/TR/html401/interact/forms.html#adef-enctype) – Cipiripi

+1

를 다시로드 할 때, UploadFile로 가치가 손실 같은 문제가 – Paul

8

HTML 파일 업로드 ASP MVC 3

모델 : (FileExtensionsAttribute가 MvcFutures에서 사용할 수 있는지 주 그것은 파일 확장자 클라이언트 측과 서버 측 유효성을 검사합니다..)

public class ViewModel 
{ 
    [Required, Microsoft.Web.Mvc.FileExtensions(Extensions = "csv", ErrorMessage = "Specify a CSV file. (Comma-separated values)")] 
    public HttpPostedFileBase File { get; set; } 
} 

HTML보기 :

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.TextBoxFor(m => m.File, new { type = "file" }) 
    @Html.ValidationMessageFor(m => m.File) 
} 

컨트롤러 액션 :

난 아직도 페이지가 당신에 enctype = "multipart/form-data"를 설정 가지고