您现在的位置是:网站首页> 编程资料编程资料

ASP.NET MVC处理文件上传的小例子_实用技巧_

2023-05-24 354人已围观

简介 ASP.NET MVC处理文件上传的小例子_实用技巧_

复制代码 代码如下:

   
 

Files uploaded to server

   
 
     
  <% using (Html.BeginForm("Upload", "File", FormMethod.Post, new 

{ 

enctype = "multipart/form-data" 

}

)) 
  {%>

 
    

 ;

 
    

     
  <% } %>   
 
Upload File 
 


然后,我们需要根据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"); 
}

相关内容

-六神源码网