用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > Net教程

ASP.NET MVC处理文件上传的小例子

时间:2015-01-29  来源:互联网  作者:佚名

复制代码 代码如下:
<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"); 
}

来顶一下
返回首页
返回首页
推荐资讯
asp.net TreeView与XML三步生成列表树 asp.net TreeView与XML三步生成列表刚我在做Tree view 绑定时自己摸索了一下,网上有人说TreeVi
asp.net中几种常用的身份验证方法总结 asp.net中几种常用的身份验证方法总前言在B/S系统开发中,经常需要使用“身份验证”。因为web应
相关文章
栏目更新
栏目热门