//前台代码(3个FileUpload,实际开发中使用js循环添加) <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default12.aspx.cs" Inherits="Default12" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /><br /> <br /> <asp:FileUpload ID="FileUpload2" runat="server" /><br /> <br /> <asp:FileUpload ID="FileUpload3" runat="server" /><br /> <br /> <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /> <br /> <asp:Label ID="label1" runat="server" Text="Label" Visible="False"></asp:Label></div> </form> </body> </html> //后台代码 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Text; public partial class Default12 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } protected void Button1_Click(object sender, EventArgs e) { try { HttpFileCollection files = Request.Files; //输出信息保存到 StringBuilder中 StringBuilder strmsg = new StringBuilder(); for (int i = 0; i < files.Count; i++) { if (files[i].FileName.Length > 0) { HttpPostedFile hpf = files[i]; if (hpf.ContentLength / 1024 > int.Parse(fun.getWebConfigInfo("UploadMaxSize")))//单个文件不能大于 { strmsg.Append(Path.GetFileName(hpf.FileName) + "---不能大于" + int.Parse(fun.getWebConfigInfo("UploadMaxSize")) / 1024 + "M<br>"); break; } string fex = Path.GetExtension(hpf.FileName).ToLower(); if (!fun.IsPermissionExt(fex)) { strmsg.Append(Path.GetFileName(hpf.FileName) + "---文件格式不对,只能是" + fun.getWebConfigInfo("UploadPermissionExt") + "<br>"); break; } if (strmsg.Length <= 0)//说明文件大小和格式都没问题 { string dirpath = Server.MapPath("~/Upload"); //如果根目录没有Upload文件夹,则创建 if (Directory.Exists(dirpath) == false) { Directory.CreateDirectory(dirpath); } //保存上传文件 hpf.SaveAs(dirpath + "//" + Path.GetFileName(hpf.FileName)); } } else { strmsg.Append("请选择上传文件!"); break; } } label1.Text = strmsg.ToString(); label1.Visible = true; } catch (Exception ex) { Response.Write(ex); } } } 类fun using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// fun 的摘要说明 /// </summary> public class fun { public fun() { } // 读取web.config文件 public static string getWebConfigInfo(string key) { return ConfigurationManager.AppSettings[key].ToString(); } //允许上传文件大小(key=UploadMaxSize) //允许上传文件类型(key=UploadPermissionExt) public static bool IsPermissionExt(string ext) { string[] exts = fun.getWebConfigInfo("UploadPermissionExt").Split(','); foreach (string str in exts) { if (str == ext) { return true; } } return false; } } //web.config <appSettings> <!--上传文件最大值,单位K--> <add key="UploadMaxSize" value="20480"/> <!--上传文件允许类型--> <add key="UploadPermissionExt" value=".jpg,.rar,.gif"/> </appSettings>