【发布时间】:2015-05-27 14:48:59
【问题描述】:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Movies.Models.StudentModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">
<script type="text/javascript">
$(document).ready(function () {
//$('.date').datepicker({ dateFormat: "dd/mm/yy", Date: Date });
$('.date').datepicker();
$('.date').datepicker("setDate", new Date());
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#UImage').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function () {
readURL(this);
});
</script>
<h2>Create</h2>
<% using (Html.BeginForm("Create", "Student", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.StudentName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.StudentName) %>
<%: Html.ValidationMessageFor(model => model.StudentName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.RollNo) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.RollNo) %>
<%: Html.ValidationMessageFor(model => model.RollNo) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.STD) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.STD, Model.STDs, "Please Select Std", new { @class = "form-control" }) %>
<%: Html.ValidationMessageFor(model => model.STD)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Address) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Address)%>
<%: Html.ValidationMessageFor(model => model.Address)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Address2) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Address2)%>
<%: Html.ValidationMessageFor(model => model.Address2)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Image) %>
</div>
<div class="editor-field">
<input type='file' id="File1" name="File1" onchange="readURL(this)" />
<img id="UImage" src="#" height="300px" width="300px" alt="your image" />
<%: Html.ValidationMessageFor(model => model.Image)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.City) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.City)%>
<%: Html.ValidationMessageFor(model => model.City)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ZIP) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ZIP)%>
<%: Html.ValidationMessageFor(model => model.ZIP)%>
<br />
<br />
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DOB) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DOB, new { @class = "date" })%>
<%: Html.ValidationMessageFor(model => model.DOB)%>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</form>
</asp:Content>
这是我的 aspx 页面,我使用 jquery 向用户显示上传的图像。
控制器代码如下
[HttpPost]
public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1)
{
Student st = new Student();
HttpPostedFileBase file;
file = Request.Files["File1"];///it only access the name of the object of html."file1" is the name of the object
if (file != null && file.ContentLength > 0)
{
byte[] Image=null;
Image = new byte[file.ContentLength];
file.InputStream.Read(Image, 0, file.ContentLength);
StudentEn.Image=Image;
}
if (ModelState.IsValid)
{
st.Insert(StudentEn);
return RedirectToAction("Index");
}
else
{
StudentEn.STDs = getSelectedSTD(GetSTDList());
return View(StudentEn);
}
}
这里是控制器代码。我总是让 request.files 为空。 我需要更改什么才能上传图像并将其存储到数据库中。
【问题讨论】:
-
方法
Create()中,第二个参数HttpPostedFileBase File1是干什么用的?我根本看不到您的代码引用它... -
为什么要将 File1 分配给文件..只需使用 File1 而不是文件并检查结果..
-
试试
Request.Files[0] -
我建议您不要将 Webforms 与 MVC 混合使用。 (即:
<asp:Content)。
标签: c# asp.net-mvc-2 image-upload