我正在尝试上传文件。我按照这些例子:
http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/ http://www.mikesdotnetting.com/article/259/asp-net-mvc-5-with-ef-6-working-with-files
当我在编辑操作中检查文件时,它为空。我哪里做错了?这是我的代码:
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int? id, FormCollection form, string addFileButton, HttpPostedFileBase file)
{
var cardKey = db.CardKeys.Include(c => c.Request).Single(c => c.CardKeyID == id);
if (addFileButton != null)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return View(cardKey);
}
UpdateRequestClearances(form["Clearances"].Split(','), cardKey.Request);
if (ModelState.IsValid)
{
db.Entry(cardKey).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", cardKey.DepartmentID);
ViewBag.DivisionID = new SelectList(db.Divisions, "DivisionID", "DivisionName", cardKey.DivisionID);
ViewBag.ProgramID = new SelectList(db.Programs, "ProgramID", "ProgramName", cardKey.ProgramID);
return View(cardKey);
}
查看:(我省略了许多不相关的代码)
@using (Html.BeginForm("Edit","CardKeys",FormMethod.Post,new { enctype="multipart/form-data"}))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CardKey</h4>
<hr />
<div class="form-group">
@Html.Label("File Upload", new { @class="control-label col-md-4"})
<div class="col-md-8">
<input type="file" multiple="multiple" id="files" />
<input type="submit" name="addFileButton" id="btnAddFile" value="add files" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
我以管理员身份打开visual studio,以确保这不是权限问题。