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);
}
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);
}
上一篇: Java编写计算器的常见方法实例总结