2012-04-19 45 views
3

我試着去保存一個類別對象在我的數據庫並上傳,我可以指代這一類的圖像,ASP.NET MVC3網頁不會把文件上傳到服務器

類別是完全保存,但圖像沒有被由於某種原因上傳。當我調試時,我可以看到我的應用程序永遠不會輸入將文件存儲在服務器上的方法,因爲我的文件是「空」。

型號:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data.Entity; 

namespace SkyLearn.Areas.Categories.Models 
{ 
    public class Category 
    { 
     public int ID { get; set; } 
     public string Title { get; set; } 
     public string Icon { get; set; } 
     public string Description { get; set; } 
    } 

    public class CategoryDBContext : DbContext 
    { 
     public DbSet<Category> categories { get; set; } 
    } 
} 

控制器:

// 
// POST: /Categories/Category/Create 
[Authorize(Roles = "administrator")] 
[HttpPost] 
public ActionResult Create(Category category, HttpPostedFileBase Icon) 
{ 
    if (Icon != null && Icon.ContentLength > 0) 
    { 
     // extract only the filename 
     var fileName = Path.GetFileName(Icon.FileName); 
     // store the file inside ~/App_Data/uploads folder 
     var path = Path.Combine(Server.MapPath("../Content/icons/"), fileName); 
     Icon.SaveAs(path); 
     category.Icon = fileName; 
    } 

    if (ModelState.IsValid) 
    { 
     db.categories.Add(category); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    return View(category); 
} 

觀點:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<SkyLearn.Areas.Categories.Models.Category>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Create 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<h2>Create</h2> 

<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script> 
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script> 

<% using (Html.BeginForm()) { %> 
<form action="" method="post" enctype="multipart/form-data"> 
    <%: Html.ValidationSummary(true) %> 
    <fieldset> 
     <legend>Category</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Title) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.EditorFor(model => model.Title) %> 
      <%: Html.ValidationMessageFor(model => model.Title) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Icon) %> 
     </div> 
     <div class="editor-field"> 
      <input type="file" name="icon" id="icon"/> 
     </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> 

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

<div> 
    <%: Html.ActionLink("Back to List", "Index") %> 
</div> 

</asp:Content> 

人誰可以告訴我,爲什麼它不會進入,節省了文件ID爲荷方法。

我試圖通過其他答案在這裏查看stackoverflow,即使他們有一些問題,因爲我的解決方案沒有解決我的問題。

我也嘗試在我的config.web中更改上傳大小等。

請幫我:)

+2

我不知道瀏覽器將如何對待一個嵌套形式,但有手動使用'Html.BeginForm()',然後寫了一個表單標籤是沒有意義的。 'Html.BeginForm()'有一個輸出Html屬性的重載:''Html.BeginForm(action,controller,FormMethod.Post,new {enctype =「multipart/form-data」})''。 – 2012-04-19 22:43:51

+1

如果你在乎添加,作爲答案,我會立即接受。這只是解決了我所有的問題:) – AronChan 2012-04-19 22:49:36

+0

謝謝,很高興它的工作。 :) – 2012-04-19 22:51:53

回答

1

我不知道瀏覽器如何處理嵌套表單,但使用Html.BeginForm()然後手動寫出表單標記沒有意義。 Html.BeginForm()具有輸出HTML屬性過載:

Html.BeginForm(action, controller, 
    FormMethod.Post, new { enctype="multipart/form-data" }) 
0

ModelBinder可能炸燬了。當您的表單按照它的設置方式(所有表單值將匹配高達Category屬性)時,您的行爲方法名稱爲Icon並且您的Category類名爲Icon也不會有輸入。

此外,表單字段名稱是小寫的「圖標」和不匹配的大寫的「圖標」,你在C#中有

你也許可以讓一個自定義類的操作方法將接收來自表單提交:

public class CategoryWithFile : Category 
{ 
    public HttpPostedFileBase IconFile { get; set; } 
} 

然後做出你的行動方法的參數

public ActionResult Create(CategoryWithFile category) 

,改變你的形式有:

<div class="editor-label"> 
    <label for="IconFile">Icon: </label> 
</div> 
<div class="editor-field"> 
    <input type="file" name="IconFile" id="IconFile"/> 
</div>