2012-05-15 4 views
2

을 게시하지 않습니다이 내 컨트롤러입니다 :는 MVC3의 이미지를 업로드 할 수 없습니다 그리고 액션 메소드

@using (Html.BeginForm("Create","ManagePhotos", FormMethod.Post, new {enctype = "multipart/form-data"})) 
{ 

@Html.ValidationSummary(true) 
<fieldset> 
    <legend>Photo</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.CategoryID, "Category") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("CategoryID", String.Empty) 
     @Html.ValidationMessageFor(model => model.CategoryID) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Name) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Name) 
     @Html.ValidationMessageFor(model => model.Name) 
    </div> 

    <div class="editor-label"> 
     @Html.Label("File name: ") 
    </div> 
    <div class="editor-field"> 
     <input type="file" name="file"/> 
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 

} 

내 질문 : 이것은 내보기입니다

public ActionResult Create() 

    { 
     ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName"); 
     return View(); 
    } 

    // 
    // POST: /ManagePhotos/Create 

    [HttpPost] 
    public ActionResult Create(Photo photo, HttpPostedFile file) 
    { 
     if (ModelState.IsValid) 
     { 
      if (file != null && file.ContentLength > 0) 
      { 
       // save as original size of image 
       var newfileName = Guid.NewGuid().ToString() + "_" 
            + Path.GetFileName(file.FileName); 
       var bigImagePath = Path.Combine(Server.MapPath("~/Content/PublicPhotos/BigImages"), newfileName); 
       file.SaveAs(bigImagePath); 

       // save as thumbnail image 
       var photoUploaded = new WebImage(bigImagePath); 
       photoUploaded.Resize(width: 200, height: 150, preserveAspectRatio: true, preventEnlarge: true); 

       var thumbImagePath = Path.Combine(Server.MapPath("~/Content/PublicPhotos/ThumbImages"), newfileName); 

       photoUploaded.Save(thumbImagePath); 

      } 

      db.Photos.Add(photo); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

: '작성'버튼을 누르면 아래 웹 페이지가 표시됩니다.

연결이 재설정되었습니다.

페이지가로드되는 동안 서버에 대한 연결이 재설정되었습니다.

일시적으로 사이트를 사용할 수 없거나 너무 바쁠 수 있습니다. 잠시 후 다시 시도하십시오. 페이지를로드 할 수없는 경우 컴퓨터의 네트워크 연결을 확인하십시오. 컴퓨터 또는 네트워크가 방화벽이나 프록시로 보호되는 경우 Firefox가 웹에 액세스 할 수 있는지 확인하십시오.

나는 면도기보기에서 디버그를 시도하고이 웹 사이트를 조회하면 웹 사이트를 정확하게 보여 주었지만 나와는 알 수 없습니다. 도와주세요.

+0

업로드하려는 파일의 크기는 얼마입니까? – fmgp

+2

파일이 4MB를 초과하는 경우 다음을 확인하십시오. http://stackoverflow.com/questions/8822860/mvc3-windows-input-type-file-size-limit – fmgp

+0

네, 맞았어요. 고맙습니다. – Brandon

답변

1

당신이 자바 스크립트에서

function onSelectImage(e) { 
    if (e.files[0].size > 256000) { 
     alert('The file size is too large for upload'); 
     e.preventDefault(); 
     return false; 
    } 
    ... 
    return true; 
} 

이 작업을 수행 할 수 있습니다 너무 큰되지 귀하의 작업이 HttpPostedFile 기대하고 있지만 크기를 확인해야합니다 의견에 명시된대로 당신은 또한

HttpPostedFileBase를 사용한다 예를 들어 이미지가 256K 이하인지 확인하십시오.

+0

@Brandon 그게 도움이되는지 알려주세요. –

관련 문제