ASP.NET MVC处理文件上传的小例子
<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>
然后,我们需要根据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");
}
相关文章
gridview checkbox从服务器端和客户端两个方面实现全选和反选
GridView中的checkbox的全选和反选在很多的地方都是要求实现的,所以下面就从服务器端和客户端两个方面实现了checkbox的选择,感兴趣的朋友可以了解下,希望本文对你有所帮助2013-01-01ASP.NET The system cannot find the file specified解决办法
这篇文章主要介绍了ASP.NET The system cannot find the file specified解决办法的相关资料,需要的朋友可以参考下2016-11-11一个Asp.Net的显示分页方法 附加实体转换和存储过程 带源码下载
现在自己写的webform都不用服务器控件了,所以自己仿照aspnetpager写了一个精简实用的返回分页显示的html方法,其他话不说了,直接上代码2012-10-10
最新评论