我正在开发 ASP.Net MVC 4 上的应用程序,并尝试以表单上传图像。我遇到的问题是,当表单发布时,如果它<input type="file"...
是空的(意味着我没有选择文件),表单发布就很好,一切正常。但是,当我选择一个文件时,表单只是坐在那里,什么也不做。
起初我以为上传需要一段时间,但我已经离开了很长一段时间(文件大小为 7kb),什么也没有。调试器甚至在操作开始时也没有命中断点。我很茫然,因为我对这个平台很陌生,而且每天都在学习。请在下面找到所有相关代码:
风景:
@using (Html.BeginForm("StaffInformation", "Manager", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="control-group">
@Html.LabelFor(model => model.firstName, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.firstName, new { @class = "span12" })
@Html.ValidationMessageFor(model => model.firstName)
</div>
</div>
<div class="control-group">
@Html.LabelFor(model => model.lastName, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.lastName, new { @class = "span12" })
@Html.ValidationMessageFor(model => model.lastName)
</div>
</div>
<div class="control-group">
Staff Photo:
<div class="controls">
@if (Model.staffImage == null)
{
@:None
}else{
<img width="150" height="150" src="@Url.Action("GetImage", "Manager", new { Model.staffProfileID })" /><br />
}
<input type="file" id="staffImage" name="staffImage" data-provide="fileinput">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<a href="@Url.Action("dashboard", "manager")" class="btn" type="reset" >Cancel</a>
</div>
}
那个行动:
public ActionResult StaffInformation(StaffInformationViewModel model, HttpPostedFileBase staffImage)
{
if (ModelState.IsValid) //This is where the breakpoint is
{
if (staffImage != null) {
model.imageMimeType = staffImage.ContentType;
model.staffImage = new byte[staffImage.ContentLength];
staffImage.InputStream.Read(model.staffImage, 0, staffImage.ContentLength);
}
using (var repo = new CompanyInformationRepository(new UniteOfWorkCompanies()))
{
var company = repo.FindCompany(User.Identity.Name);
repo.AddOrUpdateStaff(model, company);
}
return RedirectToAction("ManageStaff");
}
}
我真的不知道发生了什么,我使用它的原因仅仅enctype = "multipart/form-data"
是因为我正在阅读 Adam Freeman 的“Pro ASP.NET MVC 4”,他们说没有它就无法工作。就像我说的,我很新,需要我能得到的所有帮助。