2013-07-06 22 views
4

我正在處理列表網站,並試圖上傳圖像並將其存儲在數據庫中。ASP.NET MVC 4無法從表單中檢索文件

問題是,我似乎無法從窗體中檢索上傳的文件。

這裏是形式,

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Product</legend> 

     <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.LabelFor(model => model.Description) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Description) 
      @Html.ValidationMessageFor(model => model.Description) 
     </div> 

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

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

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

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

     <div class="editor-field"> 
      Upload Image <input type="file" name="listingImage" /> 
     </div> 

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

這裏是行動

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(Product product, HttpPostedFileBase listingImage) 
    { 
     if (listingImage == null) 
     { 
      return RedirectToAction("Index"); 
     } 
     if (ModelState.IsValid) 
     { 
      db.Products.Add(product); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(product); 
    } 

我有一個模型,一個產品上市的設置,像這樣。

public class Product 
{ 
    public int ID { get; set; } 

    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Location { get; set; } 
    public string Condition { get; set; } 
    public decimal Price { get; set; } 
    public string PostedBy { get; set; } 

    public string PhotoPath { get; set; } 
} 

現在,每次填寫表格並提交listingImage爲空,我都會返回索引。

有什麼我失蹤了嗎?

謝謝。

回答

2

您必須將表單編碼類型設置爲「multipart/form-data」,否則無法發佈文件。

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

是的,你可以是分開的模型數據定義文件,並像之前聲明控制器動作,不需要包括文件字段到模型類:

public ActionResult Create(Product product, HttpPostedFileBase listingImage) 
+0

非常感謝,這是我尋找的解決方案。 – jaooe

1

listingImage不是您的模型的一部分,然後它不會獲得與Model.xx屬性的其餘部分相同的綁定。

您可以更新Product包括HttpPostedFileBase listingImage和更改控制器動作

public ActionResult Create(Product product) 

在你的HTML,您可以添加這樣

@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype = "multipart/form-data" })){ @* Forgot this line as Alex Skalozub told in his answer *@ 
@* code omitted *@ 

    <div class="editor-field"> 
     @Html.TextBoxFor(model => model.listingImage, new {type = "file"}) 
    </div> 
} 

新的屬性文件位置和保存,你可以做一些行

var path = Path.Combine(Server.MapPath("~/images/products"),product.listingImage.FileName); 
product.listingImage.SaveAs(path); 
+0

我試着包括我的模型中的HttpPostedFileBase,但是我得到的錯誤類似於「Key不能爲null」的行。 – jaooe