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

ASP.NET
Eric的无限级Tree演示
C#中操作Excel的方法(一)
Data Access Application Block V2 类库中文文档(转贴 )
大文件上传浅谈,以及遇到的问题
做软件的困难:非技术困惑
走进C# (我的C#学习之旅)之二
Northwind中一个特别之处
.net中取当前系统的想关信息的类
写组件时需要的注释与属性书写方法
XPath序列之五
使用CommandBuilder为DataAdaper生成的Command更新数据源时的注意事项!
如何列举出网络上所有的SQL Server服务器
XPath序列之一
如何在C#里面象js一样可以直接计算字符串的值
XPath序列之三
电子秤和PC之间的数据通讯(应答)
从SQL Server中读写大数据列。
通过CDO组件对NNTP服务器发送消息
Singleton深入浅出
悲观观定SQL Server和Oracle

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


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