2011-05-16 4 views
0

MVC 프로젝트에서 파일 업로드 페이지를 만들려고합니다. 우선 이걸 로컬로 관리하고 싶습니다. 내 질문은 : 1- 여기 내 컨트롤러와보기입니다. 이 코드를 작동시키기 위해해야 ​​할 일이 있습니까? 나는 모델을 정의하거나 jquery를 사용하는 것을 의미한다. 파일이 업로드 될 때의 프로세스는 무엇인가? 나는이 문제를 디버깅 할 때Asp.Net MVC에서 파일 업로드 중

<input name="uploadFile" type="file" /> 
<input type="submit" value="Upload File" /> 

2, 그것은 컨트롤러에가는 결코 : 여기

[HttpPost] 
public ActionResult FileUpload(HttpPostedFileBase uploadFile) 
{ 
if (uploadFile.ContentLength > 0) 
      { 
       string filePath = Path.Combine(HttpContext.Server.MapPath("C:/Users/marti/../PhotoGallery/myimages"), 
       Path.GetFileName(uploadFile.FileName)); 
       uploadFile.SaveAs(filePath); 
      } 
      return View(); 
} 

은보기입니다.

@model ImageModel 
    @{ 
     ViewBag.Title = "New Image"; 
    } 
    <div class="content-form-container width-half"> 
     <form id='PropertiesForm' action='@Url.Action(ImageController.Actions.Add, ImageController.Name)' method='post' enctype='multipart/form-data' class='content-form'> 
     @Html.Partial("ImageName") 
     <fieldset class='content-form-1field'> 
      <div class='legend'> 
       file to upload 
      </div> 
      @Html.LabelledFileInput(ImageView.FileName, string.Empty) 
     </fieldset> 
     <div class='buttons'> 
      @Html.Button("button-submit", "submit") 
     </div> 
     </form> 
    </div> 
    @section script{ 
     @Html.JavascriptInclude("~/js/image/new.min.js") 
    } 

을 그리고 여기 내 컨트롤러 코드는 다음과 같습니다 :

+0

확실하지 않습니다. 작동하지 않는 것은 무엇입니까? 또는 파일 업로드 진행률을 원하십니까? – Craig

+0

게시물 요청을 통해 제출하고 컨트롤러에'FormCollection collection'을 허용하려면 Html 폼'

'태그를 사용해야합니다. –

+0

예 파일 업로드 진행이 필요했습니다. – Mgnfcnt

답변

0

당신은 당신의보기 양식에 enctype='multipart/form-data'을해야 할 수도 있습니다

[HttpPost] 
    [MemberAccess] 
    public ActionResult Add() 
    { 
     var name = ImageView.ImageName.MapFrom(Request.Form); 

     if (Request.Files.Count == 0) 
     { 
      RegisterFailureMessage("No file has been selected for upload."); 

      return ValidationFailureAdd(name); 
     } 

     var file = Request.Files[0]; 

     if (file == null || file.ContentLength == 0) 
     { 
      RegisterFailureMessage("No file has been selected for upload or the file is empty."); 

      return ValidationFailureAdd(name); 
     } 

     var format = ImageService.ImageFormat(file.InputStream); 

     if (format != ImageFormat.Gif && format != ImageFormat.Jpeg && format != ImageFormat.Png) 
     { 
      RegisterFailureMessage("Only gif, jpg and png files are supported."); 

      return ValidationFailureAdd(name); 
     } 

     if (query.HasName(name)) 
     { 
      RegisterFailureMessage(string.Format("Image with name '{0}' already exists.", name)); 

      return ValidationFailureAdd(name); 
     } 

     using (var scope = new TransactionScope()) 
     { 
      var id = Guid.NewGuid(); 

      var fileExtension = ImageService.FileExtension(format); 

      Bus.Send(new AddWikiImageCommand 
        { 
         Id = id, 
         Name = name, 
         FileExtension = fileExtension 
        }); 

      var path = Path.Combine(ApplicationConfiguration.MediaFolder, 
            string.Format("{0}.{1}", id.ToString("n"), fileExtension)); 

      if (System.IO.File.Exists(path)) 
      { 
       System.IO.File.Delete(path); 
      } 

      file.SaveAs(path); 

      scope.Complete(); 
     } 

     return RedirectToAction(Actions.Manage); 
    } 

당신이 사람들을 무시할 수 있습니다에서 일부 사용자 지정 비트가 있습니다. 당신이 필요로하는 것의 비밀은 거기 있어야합니다.

HTH

+0

@using (Html.BeginForm ("Upload", "Home", FormMethod.Post, new {@enctype = "multipart/form-data"})) 나는 이것을 고소하고 작동한다. – Mgnfcnt

+0

@Mgnfcnt : 예, 할 수 있습니다 :) --- 너무 많이 사지는 않습니다. 내가 typicall 데이터 엔트리 폼을 가지고있을 때 나에게 몇 가지 물건을 추가하는 Html.ContentForm 구현을 가지고있다. 그러나 Html.BeginForm은 확실히 옵션입니다. –

+0

도와 주셔서 감사합니다. 행운을 빕니다. – Mgnfcnt