asp.net MVC實現簡單的上傳功能
更新時間:2009年11月30日 21:01:14 作者:
MVC中上傳變得越來越容易,可是對于新手這個也還是不知道如何實現,以下方式實現MVC的上傳功能,以下2種方法都是可以實現的,其中的代碼參考了藍色小鋪和重典的文章。
方法一:
Home/Index.aspx中的代碼
<% using (Html.BeginForm("up","Home",FormMethod.Post,new{enctype="multipart/form-data"})) {%>
<input type="file" name="upfile" />
<input type ="submit" name ="upload" value ="上傳" />
<%} %>
Homecontroller中的代碼
[code]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult up(HttpPostedFileBase upfile)
{
if (upfile != null)
{
if (upfile.ContentLength > 0)
{
upfile.SaveAs("d:\\7.jpg");
}
}
return RedirectToAction("Index");
}
方法二:
Home/Index.aspx中的代碼
<form action="<%=Url.Action("upload2") %>" enctype="multipart/form-data" method="post">
<input name="up1" type="file" /><input type="submit" />
</form>
Homecontroller中的代碼
public ActionResult upload2(HttpPostedFileBase up1)
{
up1.SaveAs("d:\\8.jpg");
return Content(up1.FileName);
}
Home/Index.aspx中的代碼
復制代碼 代碼如下:
<% using (Html.BeginForm("up","Home",FormMethod.Post,new{enctype="multipart/form-data"})) {%>
<input type="file" name="upfile" />
<input type ="submit" name ="upload" value ="上傳" />
<%} %>
Homecontroller中的代碼
[code]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult up(HttpPostedFileBase upfile)
{
if (upfile != null)
{
if (upfile.ContentLength > 0)
{
upfile.SaveAs("d:\\7.jpg");
}
}
return RedirectToAction("Index");
}
方法二:
Home/Index.aspx中的代碼
復制代碼 代碼如下:
<form action="<%=Url.Action("upload2") %>" enctype="multipart/form-data" method="post">
<input name="up1" type="file" /><input type="submit" />
</form>
Homecontroller中的代碼
復制代碼 代碼如下:
public ActionResult upload2(HttpPostedFileBase up1)
{
up1.SaveAs("d:\\8.jpg");
return Content(up1.FileName);
}
相關文章
Asp.net開發(fā)之webform圖片水印和圖片驗證碼的實現方法
這篇文章主要介紹了Asp.net開發(fā)之webform圖片水印和圖片驗證碼的實現方法,實現思路分為前后臺代碼和效果展示,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10.NET讀寫Excel工具Spire.Xls使用 Excel單元格控制(3)
這篇文章主要為大家詳細介紹了.NET讀寫Excel工具Spire.Xls使用,Excel單元格控制,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11