2012-04-04 61 views
2

有此工作;在一個階段。問題是,現在下面的文本出現在現場爲數據庫中的「System.Web.HttpPostedFileWrapper」上傳圖像路徑名讀取「System.Web.HttpPostedFileWrapper」

下面的代碼是從控制器的圖像:

[HttpPost] 
    public ActionResult Create(CarAdvert caradvert, 
     HttpPostedFileBase picture1) 
    { 
     if (ModelState.IsValid) 
     { 

      if (picture1 != null) 
      { 
       string image1 = picture1.FileName; 
       caradvert.Image1 = image1; 
       var image1Path = Path.Combine(Server.MapPath("~/Content/Images"), image1); 
       picture1.SaveAs(image1Path); 
      } 


      db.CarAdverts.Add(caradvert); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

這段代碼是從創建視圖:

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

    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>CarAdvert</legend> 
     <div class="editor-field"> 

     <input type="file" name="Image1" /> 
     </div> 

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

回答

2

在控制器爲HttpPostedFileBase的paramater必須具有相同的名稱作爲輸入類型=「文件」。無論是做:

[HttpPost]  
public ActionResult Create(CarAdvert caradvert,   
HttpPostedFileBase Image1) 

<input type="file" name="picture1" /> 

應該這樣做