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

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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"); 
}

脚本宝典总结

以上是脚本宝典为你收集整理的ASP.NET MVC处理文件上传的小例子全部内容,希望文章能够帮你解决ASP.NET MVC处理文件上传的小例子所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。