2010-05-23 3 views
1

간단한 학교 포털을 만들고 있는데, 내 응용 프로그램에 이미지를 업로드하는 데 집착했습니다. 즉, 사용자가 학교 이미지를 내 서버에 업로드해야합니다. 이미지 용 디렉토리가 ./Content/입니다. 이미지 - 모든 업로드 이미지는이 디렉토리에 업로드해야합니다. 내가 코드aspnet mvc에서 간단한 이미지 업로드

 

input type="file" id="SchoolImageUrl" name="SchoolImageUrl" class="required"  
 

이 찾아보기 버튼을 m'getting 사용하여 다음, 나는 어떻게 서버에 해당 이미지를 업로드하는 방법과 내 행동 컨트롤러 것 몰라? 내가

+0

[이 실질적인 예를보십시오] (http://stackoverflow.com/questions/1653469/how-can-i-upload-a-file-and-save-it-to-a-stream-for-further- preview-using-c/1653508 # 1653508) MVC에서 간단한 업로드뿐만 아니라 [이 점을 고려하십시오] (http://stackoverflow.com/questions/851318/asp-net-mvc-1-to-many-saving-post -and-upload-files/851326 # 851326) 나중에 파일을 "첨부"하는 방법에 대해 설명합니다. –

답변

1

이 원인이 될 수 있습니다 this post

HttpPostedFileBase file = Request.Files["SchoolImageUrl"]; 

에서 봐 학교에

 

public ActionResult SchoolCreate(_ASI_School schoolToCreate, FormCollection collection) 
     { 
      if (!ModelState.IsValid) 
       return View(); 
      try 
      { 
       // TODO: Add insert logic here 
       schoolToCreate.SchoolId = Guid.NewGuid().ToString(); 
       schoolToCreate.UserId = new Guid(Request.Form["currentUser"]); 
       schoolToCreate.SchoolAddedBy = User.Identity.Name; 
       HttpPostedFileBase file = Request.Files["SchoolImageUrl"]; 
       file.SaveAs(file.FileName); 
       //schoolToCreate.SchoolImageUrl = Reuseable.ImageUpload(Request.Files["SchoolImageUrl"], Server.MapPath("../Content")); 
       //schoolToCreate.SchoolImageUrl = Path.GetFullPath(Request.Files[0].FileName); 
       schoolToCreate.SchoolImageUrl = collection["SchoolImageUrl"]; 

       UpdateModel(schoolToCreate); 
       _schoolRepository.CreateSchool(schoolToCreate); 
       //_schoolRepository.SaveToDb(); 

       return RedirectToAction("DepartmentCreate", "Department", new { userId = schoolToCreate.UserId, schoolId = schoolToCreate.SchoolId }); 
      } 
      catch 
      { 
       return View("CreationFailed"); 
      } 
     } 
 

여기 메신저에 geting 객체 referece 오류를 생성하기위한 컨트롤러를 다음했다. null 값을 얻었는지 확인하기 위해 디버깅을 했습니까?

new { enctype = "multipart/form-data" } 

그렇지 않으면 파일 데이터가 POST 요청에 전송되지 않습니다 :

3

은 Html.BeginForm이 포함되어 있습니까.