如何将图像作为表单的一部分上传到asp.net mvc5

时间:2016-09-27 05:53:34

标签: c# asp.net

我只是尝试使用图像配置文件记录学生信息。我想将图像作为表单的一部分上传到application->Image目录,并将图像名称保存到数据库中。

这是我的控制器

public ActionResult Create([Bind(Include ="StudentId,StudentName,StudentLastName,StudentPhone,StudentAge,StudentEmail,photo")] Student student , HttpPostedFileBase file)
{
    if (ModelState.IsValid)
    {
        file.SaveAs(HttpContext.Server.MapPath("~/Images/") + file.FileName);
        db.Students.Add(student);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(student);
}

在我看来

@using (Html.BeginForm("Create", "Students", FormMethod.Post, new { enctype = "multipart/form-data" }))
 {
    @Html.AntiForgeryToken()

  <div class="form-horizontal">
    <h4>Student</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.StudentName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StudentName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StudentName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.StudentLastName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StudentLastName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StudentLastName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.StudentPhone, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StudentPhone, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StudentPhone, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.StudentAge, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StudentAge, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StudentAge, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.StudentEmail, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StudentEmail, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StudentEmail, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.photo, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.photo, new { type = "file" })
            @Html.ValidationMessageFor(model => model.photo, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

但是当我上传时,它会生成错误消息

  

类型&#39; System.NullReferenceException&#39;

的例外

在这一行

  

file.SaveAs(HttpContext.Server.MapPath(&#34;〜/ Images /&#34;)+ file.FileName

请帮助我这个问题......

3 个答案:

答案 0 :(得分:1)

试试这个:

改变它:

来自

file.SaveAs(HttpContext.Server.MapPath("~/Images/") + file.FileName

file.SaveAs(HttpContext.Server.MapPath("~/Images/" + file.FileName))

答案 1 :(得分:0)

System.NullReferenceException表示此报告的代码行中有null的内容。

在这种情况下,它应该是您的HttpPostedFileBase file。 尝试在调试模式下运行应用程序,然后再次检查上载字段的名称,是否将其设置为“文件”。 MVC asp.net使用Name属性来定义参数。

在我的情况下,我使用简单的<input>来执行上传文件,使用另一个<img>来显示:

查看:

<!--Displaying: You need some code for this-->
<img src="@ViewBag.ImagePath" alt="Message picture" style="width:100%;">

<!--Uploading-->
@using (Html.BeginForm("Create", "Students", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <!-- Your other part of form -->

    <!-- Uploading -->
    <input type="file" name="file" />
}

控制器:

// extract only the filename
var fileName = Path.GetFileName(file.FileName);
// initial new path for upload file
var newSPath = Path.Combine(Server.MapPath("~/Images"), fileName);
// store file
file.SaveAs(newSPath);

答案 2 :(得分:0)

试试这个: -

public ActionResult Create([Bind(Include ="StudentId,StudentName,StudentLastName,StudentPhone,StudentAge,StudentEmail,photo")] Student student , HttpPostedFileBase photo)
{
    if (ModelState.IsValid)
    {
         var fileName=Path.GetFileName(photo.FileName);
         var path=Path.Combine(Server.MapPath("~/Images/") + fileName)
         photo.SaveAs(path);
         db.Students.Add(student);
         db.SaveChanges();
         return RedirectToAction("Index");
    }
    return View(student);
}