ASP.NET MVC处理文件上传的例子
发布时间:2016-06-23 编辑:jiaochengji.com
本文分享一个asp.net mvc下处理文件上传的小例子,有意研究mvc的朋友,可以参考下本例,希望对你有一定的帮助。
本节内容:
ASP.NET MVC处理文件上传。
1,页面部分
复制代码 代码示例:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Files uploaded to server</h2>
<div id="dialog" title="Upload files">
<% using (Html.BeginForm("Upload", "File", FormMethod.Post, new
{
enctype = "multipart/form-data"
}
))
{%>
<br />
<p><input type="file" id="fileUpload" name="fileUpload" size="23"/> ;</p><br />
<p><input type="submit" value="Upload file" /></p>
<% } %>
</div>
<a href="#" onclick="jquery('#dialog').dialog('open'); return false">Upload File</a>
</asp:content>
<h2>Files uploaded to server</h2>
<div id="dialog" title="Upload files">
<% using (Html.BeginForm("Upload", "File", FormMethod.Post, new
{
enctype = "multipart/form-data"
}
))
{%>
<br />
<p><input type="file" id="fileUpload" name="fileUpload" size="23"/> ;</p><br />
<p><input type="submit" value="Upload file" /></p>
<% } %>
</div>
<a href="#" onclick="jquery('#dialog').dialog('open'); return false">Upload File</a>
</asp:content>
2,然后,根据BeginForm中FileController和action(Upload)在指定的Controller中处理请求。
代码:
复制代码 代码示例:
public void Upload(
{
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads")
, Path.GetFileName(file.FileName));
file.SaveAs(filePath);
}
}
RedirectToAction("Index", "File");
}
{
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads")
, Path.GetFileName(file.FileName));
file.SaveAs(filePath);
}
}
RedirectToAction("Index", "File");
}
您可能感兴趣的文章:
ASP.NET MVC处理文件上传的例子
什么是MVC模式
NET MVC与WebForm的区别
每个ASP.NET MVC开发者必须了解的13个扩展点
asp.net MVC进阶学习---HtmlHelper控件解析(四)
asp.net MVC进阶学习---个性化目录结构(一)
ASP.NET MVC后台向前台传递多个对象、表的方法
简单介绍MVC思想与PHP实现MVC的方法
Javascript MVC框架简介
php文件上传代码大全(实例分享)
上一篇:asp.net读取与删除磁盘文件的实例代码
下一篇:c#线程同步的问题与实例分析
[关闭]