当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net MVC实现简单的上传功能

ASP.NET
在word中如何控制graph控件
把一个int数组的数字从小到大排列(C#)
Asp组件高级入门与精通系列之三
连接MYSQL数据库的方法及示例
SharePoint Portal Server之常见问题
软件开发中运用到的编号
VB程序员眼中的C#7
公农历转换VB类
VB程序员眼中的C#6
优化VB.NET应用程序的性能1
C#初学乍练-文本替换工具命令行版
如何得到某集合的所有子集合
VB程序员眼中的C#3
Shared Source CLI Essentials第一章第二部分
在ASP.NET使用javascript的一点小技巧
用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败解决方法
开始使用SQLServer2005,没用过MSDE,一开始还不知道怎么下手呢,呵呵
我的ASP.net学习历程有关于.dll文件的迷惑
在图片上写字 C#
Shared Source CLI Essentials第一章第一部分

ASP.NET 中的 asp.net MVC实现简单的上传功能


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 62 ::
收藏到网摘: n/a

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