ASP.NET MVC文件上传
我正在使用ASP.NET MVC并按照ASP.NET网站get-started-with-ef-using-mvc的教程的说明。我有一个设备类
I am using ASP.NET MVC and following the instructions on tutorial at ASP.NET website "getting-started-with-ef-using-mvc". I have an Equipment Class
public class Equipment
{
[Key] public int EquipID { get; set; }
public string EquipName { get; set; }
public string EquipDescription { get; set; }
public string EquipSerialNumber { get; set; }
public string EquipImage { get; set; }
public string EquipManufacturor { get; set; }
public string EquipLocation { get; set; }
public string EquipCalibrationFreq { get; set; }
public virtual ICollection<Calibration> Calibrations { get; set; }
}
我的任务是当用户添加新的设备时,然后在创建形式应该有一个文件上传控件,允许选择一个文件,并且当保存该文件时应将该文件复制到一个文件夹中,其路径应存储在FIELDEquipImage中。
My task is that when user add a new Equipment, then on the Create form there should be a fileupload control which allows to select a file and when save the record this file should be copied in a folder and its path shall be stored in FIELD "EquipImage.
这是查看的cshtml
Here is the cshtml of View
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Equipment</legend>
<div class="editor-label">
@Html.LabelFor(model => model.EquipName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EquipName)
@Html.ValidationMessageFor(model => model.EquipName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EquipDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EquipDescription)
@Html.ValidationMessageFor(model => model.EquipDescription)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EquipSerialNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EquipSerialNumber)
@Html.ValidationMessageFor(model => model.EquipSerialNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EquipImage)
</div>
<input type="file" name="file"/>
<div class="editor-label">
@Html.LabelFor(model => model.EquipManufacturor)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EquipManufacturor)
@Html.ValidationMessageFor(model => model.EquipManufacturor)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EquipLocation)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EquipLocation)
@Html.ValidationMessageFor(model => model.EquipLocation)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EquipCalibrationFreq)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EquipCalibrationFreq)
@Html.ValidationMessageFor(model => model.EquipCalibrationFreq)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
最后但并非最不重要的设备控制器
and last but not least the Equipment Controller
public ActionResult Create()
{
return View();
}
//
// POST: /Equipment/Create
// This action handles the form POST and the upload
[HttpPost]
public ActionResult Create(HttpPostedFileBase file, Equipment equipment)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/Content/EquipImages"), fileName);
file.SaveAs(path);
}
if (ModelState.IsValid)
{
db.Equipments.Add(equipment);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(equipment);
}
p>确保将表单的 enctype
属性设置为multipart / form-data。
Make sure to set the enctype
attribute of the form to "multipart/form-data".
@using (Html.BeginForm("TheAction", "TheController", FormMethod.Post, new{enctype="multipart/form-data")){
...
}