学习, And 整理了一下.
(一). 示例图片
(二). 示例代码
1
public
partial
class
UpMultiFileControl2:System.Web.UI.UserControl
2 {
3protectedvoidPage_Load(objectsender,EventArgse)
4{
5if(!Page.IsPostBack)
6{
7SaveCurrentPageFileControls();
8}
9}
10protectedvoidbtAddFile_Click(objectsender,EventArgse)
11{
12AddOneFileControl();
13}
14
15/**////<summary>
16///添加一个上传文件控件
17///</summary>
18privatevoidAddOneFileControl()
19{
20ArrayListal=newArrayList();
21this.tbFiles.Rows.Clear();
22GetFileControlsFromSession();
23HtmlTableRowhtr=newHtmlTableRow();
24HtmlTableCellhtc=newHtmlTableCell();
25htc.Controls.Add(newFileUpload());
26htr.Controls.Add(htc);
27this.tbFiles.Rows.Add(htr);
28SaveCurrentPageFileControls();
29}
30
31/**////<summary>
32///读取缓存中存储的上传文件控件集
33///</summary>
34privatevoidGetFileControlsFromSession()
35{
36ArrayListal=newArrayList();
37if(Session["FilesControls"]!=null)
38{
39al=(System.Collections.ArrayList)Session["FilesControls"];
40for(inti=0;i<al.Count;i++)
41{
42HtmlTableRowhtr1=newHtmlTableRow();
43HtmlTableCellhtc1=newHtmlTableCell();
44htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
45htr1.Controls.Add(htc1);
46this.tbFiles.Rows.Add(htr1);
47}
48}
49}
50
51/**////<summary>
52///保存当前页面上传文件控件集到缓存中
53///</summary>
54privatevoidSaveCurrentPageFileControls()
55{
56ArrayListal=newArrayList();
57foreach(ControlcontrolTRinthis.tbFiles.Controls)
58{
59if(controlTR.GetType().ToString()=="System.Web.UI.HtmlControls.HtmlTableRow")
60{
61HtmlTableCellhtc=(HtmlTableCell)controlTR.Controls[0];
62foreach(ControlcontrolFileUploadinhtc.Controls)
63{
64if(controlFileUpload.GetType().ToString()=="System.Web.UI.WebControls.FileUpload")
65{
66FileUploadtempFileUpload=(FileUpload)controlFileUpload;
67al.Add(tempFileUpload);
68}
69}
70}
71}
72Session.Add("FilesControls",al);
73}
74
75protectedvoidbtUpFiles_Click(objectsender,EventArgse)
76{
77UpLoadFiles();
78}
79
80/**////<summary>
81///上传文件操作
82///</summary>
83privatevoidUpLoadFiles()
84{
85stringfilepath=Server.MapPath("./")+"UploadFiles";
86
87HttpFileCollectionuploadedFiles=Request.Files;
88for(inti=0;i<uploadedFiles.Count;i++)
89{
90HttpPostedFileuserPostedFile=uploadedFiles[i];
91try
92{
93if(userPostedFile.ContentLength>0)
94{
95userPostedFile.SaveAs(filepath+"\\"+System.IO.Path.GetFileName(userPostedFile.FileName));
96Response.Write("已上传文件:\""+filepath+"\\"+userPostedFile.FileName+"\"<br><br>");
97}
98}
99catch
100{
101Response.Write("上传文件:\""+userPostedFile.FileName+"\"出错!");
102}
103}
104if(Session["FilesControls"]!=null)
105{
106Session.Remove("FilesControls");
107}
108}
109}
2 {
3protectedvoidPage_Load(objectsender,EventArgse)
4{
5if(!Page.IsPostBack)
6{
7SaveCurrentPageFileControls();
8}
9}
10protectedvoidbtAddFile_Click(objectsender,EventArgse)
11{
12AddOneFileControl();
13}
14
15/**////<summary>
16///添加一个上传文件控件
17///</summary>
18privatevoidAddOneFileControl()
19{
20ArrayListal=newArrayList();
21this.tbFiles.Rows.Clear();
22GetFileControlsFromSession();
23HtmlTableRowhtr=newHtmlTableRow();
24HtmlTableCellhtc=newHtmlTableCell();
25htc.Controls.Add(newFileUpload());
26htr.Controls.Add(htc);
27this.tbFiles.Rows.Add(htr);
28SaveCurrentPageFileControls();
29}
30
31/**////<summary>
32///读取缓存中存储的上传文件控件集
33///</summary>
34privatevoidGetFileControlsFromSession()
35{
36ArrayListal=newArrayList();
37if(Session["FilesControls"]!=null)
38{
39al=(System.Collections.ArrayList)Session["FilesControls"];
40for(inti=0;i<al.Count;i++)
41{
42HtmlTableRowhtr1=newHtmlTableRow();
43HtmlTableCellhtc1=newHtmlTableCell();
44htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
45htr1.Controls.Add(htc1);
46this.tbFiles.Rows.Add(htr1);
47}
48}
49}
50
51/**////<summary>
52///保存当前页面上传文件控件集到缓存中
53///</summary>
54privatevoidSaveCurrentPageFileControls()
55{
56ArrayListal=newArrayList();
57foreach(ControlcontrolTRinthis.tbFiles.Controls)
58{
59if(controlTR.GetType().ToString()=="System.Web.UI.HtmlControls.HtmlTableRow")
60{
61HtmlTableCellhtc=(HtmlTableCell)controlTR.Controls[0];
62foreach(ControlcontrolFileUploadinhtc.Controls)
63{
64if(controlFileUpload.GetType().ToString()=="System.Web.UI.WebControls.FileUpload")
65{
66FileUploadtempFileUpload=(FileUpload)controlFileUpload;
67al.Add(tempFileUpload);
68}
69}
70}
71}
72Session.Add("FilesControls",al);
73}
74
75protectedvoidbtUpFiles_Click(objectsender,EventArgse)
76{
77UpLoadFiles();
78}
79
80/**////<summary>
81///上传文件操作
82///</summary>
83privatevoidUpLoadFiles()
84{
85stringfilepath=Server.MapPath("./")+"UploadFiles";
86
87HttpFileCollectionuploadedFiles=Request.Files;
88for(inti=0;i<uploadedFiles.Count;i++)
89{
90HttpPostedFileuserPostedFile=uploadedFiles[i];
91try
92{
93if(userPostedFile.ContentLength>0)
94{
95userPostedFile.SaveAs(filepath+"\\"+System.IO.Path.GetFileName(userPostedFile.FileName));
96Response.Write("已上传文件:\""+filepath+"\\"+userPostedFile.FileName+"\"<br><br>");
97}
98}
99catch
100{
101Response.Write("上传文件:\""+userPostedFile.FileName+"\"出错!");
102}
103}
104if(Session["FilesControls"]!=null)
105{
106Session.Remove("FilesControls");
107}
108}
109}
(三). 改变上传文件大小和时间限制
<httpRuntime>
executionTimeout="110" //允许上传文件最大等待时间
maxRequestLength="4096" //上传文件大小,默认为4M
</httpRuntime>
上传文件大小是由上面两个参数所决定的. 涉及到安全因素,最好不要设得太大.
(四). 示例源代码下载
http://www.cnblogs.com/Files/ChengKing/UpMultiFileControl.rar
(五).控件缺点
由于安全原因, 用服务端控件实现,回发时选择的文件不能够保存.