MVC 4 圖片的上傳及顯示

來源:互聯網
上載者:User

標籤:style   http   color   os   io   strong   ar   for   art   

首先我們看一下如何上傳:

1. view

 

上傳頁面:

  1: @using (Html.BeginForm("Create", "Achievement", FormMethod.Post, new { enctype = "multipart/form-data" }))
  2: {
  3:      <div class="editor-label">
  4:             @Html.LabelFor(model => model.Pictures)
  5:         </div>
  6:         <div class="editor-field">
  7:             <div><input type="file" name="Image" /></div>
  8:         </div>
  9: }
這裡需要注意的是BeginForm方法的參數
 
2. control
 
  1:  public ActionResult Create(Achivement achieve, HttpPostedFileBase image)
  2:         {
  3:             try
  4:             {
  5:                 
  6:                 if (image != null && image.ContentLength > 0)
  7:                 {
  8:                     string fileName = DateTime.Now.ToString("yyyyMMdd") + "-" + Path.GetFileName(image.FileName);
  9:                     string filePath = Path.Combine(Server.MapPath("~/Images"), fileName);
 10:                     image.SaveAs(filePath);
 11:                     achieve.Pictures = "~/Images/" + fileName ; 
 12:                 }
 13:                 m_achivementService.Create(achieve);
 14:                 return RedirectToAction("Index");
 15:             }
 16:             catch
 17:             {
 18:                 return View();
 19:             }
 20:         }
 
現在圖片已上傳到Images目錄下,注意這裡Pictures欄位存的圖片路徑一定要帶上“~”。
 
現在我們來看下如何顯示:
1. view
  1: @using (Html.BeginForm("Edit", "Achievement", FormMethod.Post, new { enctype = "multipart/form-data" }))
  2: {
  3:    <div class="editor-label">
  4:             @Html.LabelFor(model => model.Pictures)
  5:         </div>
  6:         <div class="editor-field">
  7:             @*@Html.EditorFor(model => model.Pictures)
  8:             @Html.ValidationMessageFor(model => model.Pictures)*@
  9:             <div><input type="file" name="Image" /></div>
 10:             <div>
 11:                 @if (string.IsNullOrEmpty(Model.Pictures))
 12:                 {
 13:                     @:None
 14:                 }
 15:                 else
 16:                 {
 17:                       <img width="150" height="150" src="@Url.Content(Model.Pictures)" alt="images" />
 18:                 }
 19:             </div>
 20:         </div>
 21: }
這裡需要注意的是src的地方,不能直接寫上Model.Pictures,前面要加上@Url.Content, 不然顯示的是c:/images/123.png, 圖片不能正常顯示。
 
control:
跟create一樣的操作, 此處省略。

MVC 4 圖片的上傳及顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.