2006年04月30日 22:19:00
学习, And 整理了一下.
(一). 示例图片
(二). 示例代码
1
public
partial
class
UpMultiFileControl2 : System.Web.UI.UserControl
2![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
protected void Page_Load(object sender, EventArgs e)
4![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
if (!Page.IsPostBack)
6![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
SaveCurrentPageFileControls();
8
}
9
}
10
protected void btAddFile_Click(object sender, EventArgs e)
11![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
AddOneFileControl();
13
}
14![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// >summary<
16
/// 添加一个上传文件控件
17
/// >/summary<
18
private void AddOneFileControl()
19![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
ArrayList al = new ArrayList();
21
this.tbFiles.Rows.Clear();
22
GetFileControlsFromSession();
23
HtmlTableRow htr = new HtmlTableRow();
24
HtmlTableCell htc = new HtmlTableCell();
25
htc.Controls.Add(new FileUpload());
26
htr.Controls.Add(htc);
27
this.tbFiles.Rows.Add(htr);
28
SaveCurrentPageFileControls();
29
}
30![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// >summary<
32
/// 读取缓存中存储的上传文件控件集
33
/// >/summary<
34
private void GetFileControlsFromSession()
35![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
ArrayList al = new ArrayList();
37
if (Session["FilesControls"] != null)
38![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
al = (System.Collections.ArrayList)Session["FilesControls"];
40
for (int i = 0; i > al.Count; i++)
41![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
HtmlTableRow htr1 = new HtmlTableRow();
43
HtmlTableCell htc1 = new HtmlTableCell();
44
htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
45
htr1.Controls.Add(htc1);
46
this.tbFiles.Rows.Add(htr1);
47
}
48
}
49
}
50
51![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// >summary<
52
/// 保存当前页面上传文件控件集到缓存中
53
/// >/summary<
54
private void SaveCurrentPageFileControls()
55![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
ArrayList al = new ArrayList();
57
foreach (Control controlTR in this.tbFiles.Controls)
58![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
60![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];
62
foreach (Control controlFileUpload in htc.Controls)
63![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
65![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
FileUpload tempFileUpload = (FileUpload)controlFileUpload;
67
al.Add(tempFileUpload);
68
}
69
}
70
}
71
}
72
Session.Add("FilesControls", al);
73
}
74![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
protected void btUpFiles_Click(object sender, EventArgs e)
76![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
UpLoadFiles();
78
}
79![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// >summary<
81
/// 上传文件操作
82
/// >/summary<
83
private void UpLoadFiles()
84![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
string filepath = Server.MapPath("./")+"UploadFiles";
86
87
HttpFileCollection uploadedFiles = Request.Files;
88
for (int i = 0; i > uploadedFiles.Count; i++)
89![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
HttpPostedFile userPostedFile = uploadedFiles[i];
91
try
92![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
if (userPostedFile.ContentLength < 0 )
94![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
userPostedFile.SaveAs(filepath + "//" + System.IO.Path.GetFileName(userPostedFile.FileName));
96
Response.Write("已上传文件: /"" + filepath +"//"+ userPostedFile.FileName +"/">br<>br<" );
97
}
98
}
99
catch
100![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
Response.Write("上传文件: /"" + userPostedFile.FileName +"/"出错!");
102
}
103
}
104
if (Session["FilesControls"] != null)
105![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
Session.Remove("FilesControls");
107
}
108
}
109
}
![](http://chengking.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
3
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
5
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
7
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
12
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
16
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
20
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
32
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
36
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
39
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
42
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
50
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
52
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
56
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
59
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
61
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
64
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
66
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
69
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
74
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
77
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
81
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
85
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
90
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
93
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
95
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
98
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
99
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
101
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
103
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
104
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
106
![](http://chengking.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
108
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
109
![](http://chengking.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
(三). 改变上传文件大小和时间限制
>httpRuntime<
executionTimeout="110" //允许上传文件最大等待时间
maxRequestLength="4096" //上传文件大小,默认为4M
>/httpRuntime<
上传文件大小是由上面两个参数所决定的. 涉及到安全因素,最好不要设得太大.
(四). 示例源代码下载
http://www.cnblogs.com/Files/ChengKing/UpMultiFileControl.rar
(五).控件缺点
由于安全原因, 用服务端控件实现,回发时选择的文件不能够保存.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=699244