如何在单击中将多个图像保存到mvc 5 razor中的数据库和文件夹中

时间:2016-03-15 07:13:27

标签: asp.net-mvc entity-framework-5

我对此感到困惑,我必须在单个帖子中将多个图像保存到数据库和文件夹中

我的观点是

<div class="form-group">
    @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-3 col-sm-3 col-xs-12" })
   <div class="col-md-6 col-sm-6 col-xs-12">
        @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control col-md-7 col-xs-12" } })
        @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-3 col-sm-3 col-xs-12" })
   <div class="col-md-6 col-sm-6 col-xs-12">
        @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control col-md-7 col-xs-12" } })
        @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.Driverimage, htmlAttributes: new { @class = "control-label col-md-3 col-sm-3 col-xs-12" })
    <div class="col-md-6 col-sm-6 col-xs-12">
        @Html.TextBoxFor(Model => Model.Driverimage, new { type = "file" })
        @Html.ValidationMessage("Custom Error")
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.DriverLiscenseimage, htmlAttributes: new { @class = "control-label col-md-3 col-sm-3 col-xs-12" })
    <div class="col-md-6 col-sm-6 col-xs-12">
        @Html.TextBoxFor(Model => Model.DriverLiscenseimage, new { type = "file" })
        @Html.ValidationMessage("Custom Error")
    </div>
</div>

我的控制器是

      [HttpPost]
    public ActionResult RegisterDriver(DriverRegistration newRecord,HttpPostedFileBase file)
    {

        if (file != null)
        {
            string ImageName = System.IO.Path.GetFileName(file.FileName);
            string physicalPath = Server.MapPath("~/images/" + ImageName);

            // save image in folder
            file.SaveAs(physicalPath);

            //save new record in database

            newRecord.FirstName = newRecord.FirstName;

            newRecord.DriverFilename = ImageName;
            objentity.DriverRegistrations.Add(newRecord);
            objentity.SaveChanges();

        }
        return View();
    }

请帮助我.... 我把它保存在图像文件夹中但只保存了单个图像

0 个答案:

没有答案