欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-09 11:25:17
方法一: home/index.aspx中的代码 复制代码 代码如下: <% using (html.beginform("up","home",formmethod...
方法一:
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);
}