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

ASP.NET
妙用Cache检验用户是否重复登陆
ASP.NET 2.0–善用DataSourceMode属性
在ASP.NET AJAX中别使用mode="Legacy"
探讨ASP.NET 2.0的Web控件改进之概述
asp.net创建文件夹的IO类的问题
asp.net 实现购物车详细代码
ASP.NET2.0+SQL Server2005构建多层应用
用ICallbackEventHandler实现客户端与服务器端异步
ASP.NET页面的重定向
抢先试用ASP.NET 2.0中的新型安全控件
ASP.NET中Cookie编程的基础知识
Asp.net导航控件真的值得用吗?
ASP.NET中上传文件到数据库
cs及前身asp.net forums的调试
ASP.NET2.0 遍历文件夹下所有图片
用ASP.NET创建自定义文本框
ASP.NET中设计带事件定制控件
ASP.NET+ORACLE添加记录让ID自动增量
C#+ASP.NET开发基于Web的RSS阅读器
ASP.NET2.0导航功能之配置会员和角色

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 61 ::
收藏到网摘: 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);
}