复制代码 代码如下:
<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,MVC,文件上传

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“ASP.NET MVC处理文件上传的小例子”

暂无“ASP.NET MVC处理文件上传的小例子”评论...