我試着去保存一個類別對象在我的數據庫並上傳,我可以指代這一類的圖像,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中更改上傳大小等。
請幫我:)
我不知道瀏覽器將如何對待一個嵌套形式,但有手動使用'Html.BeginForm()',然後寫了一個表單標籤是沒有意義的。 'Html.BeginForm()'有一個輸出Html屬性的重載:''Html.BeginForm(action,controller,FormMethod.Post,new {enctype =「multipart/form-data」})''。 – 2012-04-19 22:43:51
如果你在乎添加,作爲答案,我會立即接受。這只是解決了我所有的問題:) – AronChan 2012-04-19 22:49:36
謝謝,很高興它的工作。 :) – 2012-04-19 22:51:53