ASP.NET上传多个文件

HttpFileCollection与HttpPostedFile这二个类的组合就可以完成

前端

<asp:FileUpload ID="FileUpload1" runat="server" />
        
<br />
        
<asp:FileUpload ID="FileUpload2" runat="server" />
        
<br />
        
<asp:FileUpload ID="FileUpload3" runat="server" />
        
<br />
        
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        
<br />
        
<asp:Label ID="Label1" runat="server"></asp:Label>

后端

string filePath = Server.MapPath(""+ "\\UploadFiles\\";

            HttpFileCollection uploadFiles 
= Request.Files;
            
for (int i = 0; i < uploadFiles.Count; i++)
            {
                HttpPostedFile userPostFile 
= uploadFiles[i];
                    
try
                    {
                        
if (userPostFile.ContentLength > 0)
                        {
                            userPostFile.SaveAs(filePath 
+ Path.GetFileName(userPostFile.FileName));
                            Label1.Text 
= "上传成功!";
                        }
                    }
                    
catch (Exception ex)
                    {
                        Label1.Text 
= "Error: " + ex.Message;
                    }
            }
原文地址:https://www.cnblogs.com/whtydn/p/2060796.html