2011-01-22 1 views
2

페이지 코드를 작동하지 않는 파일 업로드ASP.NET MVC3는

<% using (Html.BeginForm()) 
    { %> 
<fieldset> 
    <legend>上传项目材料</legend> 
    <input type="file" name="File1" /> 
    <input type="submit" value="上传" /> 
</fieldset> 
<%} %> 

액션 코드

[HttpPost] 
public ActionResult FileUpLoad(int id, FormCollection form) 
{ 
    try 
    { 
     var model = db.ProjcetDeclare.First(c => c.id == id); 

     if (Request.Files.Count==0) 
     { 
      return View(); 
     } 
     string newFile=string.Empty; 

     var File1 = Request.Files[0]; 
     if (File1.ContentLength == 0) 
     { 
     } 
     newFile = model.Project.pname + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(File1.FileName); 
     File1.SaveAs(Server.MapPath("/项目材料/" + newFile)); 

     model.XMCL = "/项目材料/" + newFile; 
     UpdateModel(model); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

내가 시도하고 있지만 Request.Files.Count == 0 발견하지 파일 이유는 사실이다 ?

답변

17

이번 주 초에 만났습니다. 양식 태그에 HTML 속성 (enctype)이 누락되어 서버에 파일을 게시하고 있음을 알립니다. 여기에 해결책이 있습니다 ...

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