如何上传图像与asp.net mvc5中的其他字段的表单的一部分

本文关键字:其他 字段 一部分 表单 mvc5 net 图像 asp | 更新日期: 2023-09-27 18:09:02

我只是想用图像配置文件记录学生信息。我想将图像作为表单的一部分上传到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);
}

in my view

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

但是当我上传时,它会生成一个错误信息

类型为'System '的异常。得到NullReferenceException '

在这一行

file. saveas (HttpContext.Server.MapPath("~/Images/") + file。文件名

如何上传图像与asp.net mvc5中的其他字段的表单的一部分

试试这个:

改变:

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

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

System.NullReferenceException表示在报告的代码行中存在null

在这种情况下,它应该是您的HttpPostedFileBase file。尝试在调试模式下运行应用程序,并再次检查上传字段的名称,是否设置为"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);

试试这个:-

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