通过asp.net mvc 5中的数据库上传和显示图像

时间:2015-12-27 15:26:55

标签: c# asp.net-mvc-5

查看:

<footer>
 <div class="wrapper">
  <div id="footer">
   <div class="foot-r">
    <ul style="float:right;">
      <li style="list-style-type: none; float: left; display: inline-block; margin-right: 15px;">
     <!-- TOP.GE COUNTER CODE -->
       <div style="position: relative; width: 88px;">
        <div style="background:url('/pngs/hat_2.png');position:absolute;width:38px;height:52px;overflow:hidden;margin-left:-13px;margin-top:-6px"></div>
         <script language="JavaScript" type="text/javascript" src="//counter.top.ge/cgi-bin/cod?100+103590"></script>
         <noscript>
         <a target="_top" href="http://counter.top.ge/cgi-bin/showtop?103590">
         <img src="//counter.top.ge/cgi-bin/count?ID:103590+JS:false" border="0" alt="TOP.GE" /></a>
         </noscript>
        </div>
       <!-- / END OF TOP.GE COUNTER CODE -->
      </li>
      <li style="list-style-type: none; float: left; display: inline-block;">
  <div style="position: relative; width: 88px;">
       <div style="background:url('/pngs/hat_3.png');position:absolute;width:38px;height:52px;overflow:hidden;margin-left:-13px;margin-top:-6px"></div>
        $COUNTER$
       </div>
      </div>
     </li>
     </ul>
   </div>

   <div class="foot-l">
<!-- <copy> -->Tech Info <img src="/icons/ic_copyright_black_48dp_1x.png" alt"copyright" width="15" height="15"/> $YEAR$<!-- </copy> -->$POWERED_BY$   
  </div>
    <div class="clr"></div>
  </div>
 </div>
</footer>

控制器操作:

@using (Html.BeginForm("AddDetails", "UsersDetails", null,
                FormMethod.Post, new
                {
                    encrypt = "multipart/form-data",
                    @class = "form-horizontal",
                    role = "form"
                }))
{ 
    @Html.AntiForgeryToken()
    <label for="Picture">Upload Image:</label>
    <input name="Picture" id="Picture" type="file" />
}

当我尝试上传图片时,然后在上传输入框中,它包含整个路径public ActionResult AddDetails(UsersDetailViewModel udv, FormCollection fc, HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) try { string path = Path.Combine(Server.MapPath("~/Uploads"), Path.GetFileName(file.FileName)); file.SaveAs(path); ViewBag.Message = "File uploaded successfully"; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } else { ViewBag.Message = "You have not specified a file."; } ud.Picture = udv.Picture; return View(); } ,但我只需将图片名称作为黑色图片。

1 个答案:

答案 0 :(得分:0)

Path.GetFileName您将获得带扩展名的文件名(例如:&#34; black.png&#34;)。

if (file != null && file.ContentLength > 0)
{
   var fileName= Path.GetFileName(file.FileName);
   // It's a good idea to remove spaces
   fileName=fileName.Replace(" ","_");
   //If you want to generate a unique filename, you may try something like
   // fileName=Guid.NewGuid().ToString+fileName;

   var path = Path.Combine(Server.MapPath("~/Uploads"),fileName);
   file.SaveAs(path);

   //you can use fileName value to save to user record.
}