且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC文件上传

更新时间:2023-02-17 12:41:55

确认设置窗体的是enctype 属性为multipart / form-data的。

  @using(Html.BeginForm(TheAction,TheController,FormMethod.Post,新{ENCTYPE =多部分/表单数据)){
...
}

请参阅包括剃刀表格视图

档上传>

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; }
    }

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.

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);


    }

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")){
...
}

See Including File Upload in Razor Form View