学习, and 整理了一下.
(一). 示例图片

(二). 示例代码
1
public partial class upmultifilecontrol2 : system.web.ui.usercontrol
2
{
3
protected void page_load(object sender, eventargs e)
4
{
5
if (!page.ispostback)
6
{
7
savecurrentpagefilecontrols();
8
}
9
}
10
protected void btaddfile_click(object sender, eventargs e)
11
{
12
addonefilecontrol();
13
}
14
15
/// <summary>
16
/// 添加一个上传文件控件
17
/// </summary>
18
private void addonefilecontrol()
19
{
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
31
/// <summary>
32
/// 读取缓存中存储的上传文件控件集
33
/// </summary>
34
private void getfilecontrolsfromsession()
35
{
36
arraylist al = new arraylist();
37
if (session["filescontrols"] != null)
38
{
39
al = (system.collections.arraylist)session["filescontrols"];
40
for (int i = 0; i < al.count; i++)
41
{
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
/// <summary>
52
/// 保存当前页面上传文件控件集到缓存中
53
/// </summary>
54
private void savecurrentpagefilecontrols()
55
{
56
arraylist al = new arraylist();
57
foreach (control controltr in this.tbfiles.controls)
58
{
59
if (controltr.gettype().tostring() == "system.web.ui.htmlcontrols.htmltablerow")
60
{
61
htmltablecell htc = (htmltablecell)controltr.controls[0];
62
foreach (control controlfileupload in htc.controls)
63
{
64
if (controlfileupload.gettype().tostring() == "system.web.ui.webcontrols.fileupload")
65
{
66
fileupload tempfileupload = (fileupload)controlfileupload;
67
al.add(tempfileupload);
68
}
69
}
70
}
71
}
72
session.add("filescontrols", al);
73
}
74
75
protected void btupfiles_click(object sender, eventargs e)
76
{
77
uploadfiles();
78
}
79
80
/// <summary>
81
/// 上传文件操作
82
/// </summary>
83
private void uploadfiles()
84
{
85
string filepath = server.mappath("./")+"uploadfiles";
86
87
httpfilecollection uploadedfiles = request.files;
88
for (int i = 0; i < uploadedfiles.count; i++)
89
{
90
httppostedfile userpostedfile = uploadedfiles[i];
91
try
92
{
93
if (userpostedfile.contentlength > 0 )
94
{
95
userpostedfile.saveas(filepath + "\\" + system.io.path.getfilename(userpostedfile.filename));
96
response.write("已上传文件: \"" + filepath +"\\"+ userpostedfile.filename +"\"<br><br>" );
97
}
98
}
99
catch
100
{
101
response.write("上传文件: \"" + userpostedfile.filename +"\"出错!");
102
}
103
}
104
if (session["filescontrols"] != null)
105
{
106
session.remove("filescontrols");
107
}
108
}
109
}
public partial class upmultifilecontrol2 : system.web.ui.usercontrol2
{3
protected void page_load(object sender, eventargs e)4
{5
if (!page.ispostback)6
{7
savecurrentpagefilecontrols();8
}9
}10
protected void btaddfile_click(object sender, eventargs e)11
{12
addonefilecontrol();13
}14

15
/// <summary>16
/// 添加一个上传文件控件17
/// </summary>18
private void addonefilecontrol()19
{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

31
/// <summary>32
/// 读取缓存中存储的上传文件控件集33
/// </summary>34
private void getfilecontrolsfromsession()35
{36
arraylist al = new arraylist(); 37
if (session["filescontrols"] != null)38
{39
al = (system.collections.arraylist)session["filescontrols"];40
for (int i = 0; i < al.count; i++)41
{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
/// <summary>52
/// 保存当前页面上传文件控件集到缓存中53
/// </summary> 54
private void savecurrentpagefilecontrols()55
{ 56
arraylist al = new arraylist(); 57
foreach (control controltr in this.tbfiles.controls)58
{59
if (controltr.gettype().tostring() == "system.web.ui.htmlcontrols.htmltablerow")60
{61
htmltablecell htc = (htmltablecell)controltr.controls[0];62
foreach (control controlfileupload in htc.controls)63
{64
if (controlfileupload.gettype().tostring() == "system.web.ui.webcontrols.fileupload")65
{66
fileupload tempfileupload = (fileupload)controlfileupload;67
al.add(tempfileupload);68
}69
}70
} 71
} 72
session.add("filescontrols", al);73
}74

75
protected void btupfiles_click(object sender, eventargs e)76
{77
uploadfiles();78
}79

80
/// <summary>81
/// 上传文件操作82
/// </summary>83
private void uploadfiles()84
{85
string filepath = server.mappath("./")+"uploadfiles";86
87
httpfilecollection uploadedfiles = request.files; 88
for (int i = 0; i < uploadedfiles.count; i++)89
{ 90
httppostedfile userpostedfile = uploadedfiles[i]; 91
try92
{ 93
if (userpostedfile.contentlength > 0 )94
{ 95
userpostedfile.saveas(filepath + "\\" + system.io.path.getfilename(userpostedfile.filename));96
response.write("已上传文件: \"" + filepath +"\\"+ userpostedfile.filename +"\"<br><br>" ); 97
} 98
} 99
catch100
{101
response.write("上传文件: \"" + userpostedfile.filename +"\"出错!");102
} 103
}104
if (session["filescontrols"] != null)105
{106
session.remove("filescontrols");107
}108
} 109
}
(三). 改变上传文件大小和时间限制
<httpruntime>
executiontimeout="110" //上传等待时间
maxrequestlength="4096" //上传文件大小,默认为4m
</httpruntime>
上传文件大小是由上面两个参数所决定的. 涉及到安全因素,不要设得太大.
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!






