2016-09-27 11 views
0

我只是想記錄學生信息與圖像配置文件。我想將圖像作爲表格的一部分上載到application->Image目錄並將圖像名稱保存到數據庫。如何上傳圖像作爲與asp.net中的其他領域的窗體的一部分mvc5

這是我的控制器

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

但是當我上傳時,它產生一個錯誤消息

類型 'System.NullReferenceException' 的例外

在這條線

file.SaveAs(HttpContext.Server.MapPath( 「〜/圖片/」)+ file.FileName

請幫我這個問題,請...

回答

1

試試這個:

改變它:

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

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

感謝您的回答。但仍然得到相同的錯誤「對象引用未設置爲對象的實例」。在同一行上 –

+0

你的項目中是否有名爲Images的文件夾? – redzsol

+0

當然是。並且它位於根目錄 –

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