
MutiLoad控件
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Web.UI.MobileControls;
11
using System.Collections.Generic;
12
13
14
namespace WebApplication1
15

{
16
public class MutiLoad:WebControl
17
{
18
string strFiles = "";
19
List<int> list = new List<int>();
20
21
public int Count
22
{
23
get
24
{
25
26
return list.Count;
27
}
28
}
29
public int[] FileIndex
30
{
31
get
32
{
33
return list.ToArray();
34
}
35
}
36
protected override void OnLoad(EventArgs e)
37
{
38
base.OnLoad(e);
39
if (Page.IsPostBack)
40
{
41
if (Page.Request.Form["hidvalue"] == null)
42
strFiles ="";
43
else
44
strFiles = ","+Page.Request.Form["hidvalue"].Trim(',')+",";
45
HttpFileCollection files1 =HttpContext.Current.Request.Files;
46
for (int i = 0; i < files1.Count; i++)
47
{
48
if (CheckFile(files1[i].FileName))
49
list.Add(i);
50
}
51
52
}
53
}
54
private bool CheckFile(string FileName)
55
{
56
if (strFiles.IndexOf("," + FileName + ",") == -1)
57
return false;
58
strFiles = strFiles.Replace(FileName + ",","");
59
return true;
60
}
61
protected override void OnPreRender(EventArgs e)
62
{
63
if (!Page.ClientScript.IsClientScriptBlockRegistered("MyJavaScript"))
64
{
65
Page.ClientScript.RegisterClientScriptBlock
66
(Page.GetType(), "MyJavaScript", strScript);
67
}
68
base.OnPreRender(e);
69
HtmlForm form = this.Page.Form;
70
if ((form != null) && (form.Enctype.Length == 0))
71
{
72
form.Enctype = "multipart/form-data";
73
}
74
}
75
protected override void Render(HtmlTextWriter writer)
76
{
77
base.Render(writer);
78
writer.Write("<input type='hidden' id='hidvalue' name='hidvalue' />");
79
writer.Write(strTable);
80
}
81
82
WriteTable#region WriteTable
83
private readonly string strTable = @" <table border='0' cellpadding='0' cellspacing='0' width='100%'>
84
<tr>
85
<td>
86
<p id='MyFile'>
87
<span num='1'>
88
<input type='file' size='60' name='File' style='width:100%;height:25px' /></span></p>
89
</td>
90
<td align='left' style='width:100px'>
91
<input type='button' value='增加' onclick='addFile()' />
92
<input type='button' value='删除'
93
onclick='javascript:removeOptionSelected()' />
94
</td>
95
</tr>
96
<tr>
97
<td colspan='2' align='left'>
98
<select size='10' name='SelectListBox' id='SelectListBox' style='width:100%'>
99
</select>
100
</td>
101
</tr>
102
</table>";
103
#endregion
104
script#region script
105
private readonly string strScript = @"<script language='JavaScript' type='text/javascript'>
106
<!--
107
var count =1;
108
var SelectControlID='SelectListBox';
109
function addFile()
110
{
111
112
if(!CheckFile())
113
return;
114
count++;
115
var str = ""<span Num='""+count+""'><INPUT type='file' size='60' NAME='File' style='width:100%;height:25px'/></span>"";
116
document.getElementById('MyFile').insertAdjacentHTML('beforeEnd',str)
117
}
118
function CheckFile()
119
{
120
var filenames = document.getElementById('MyFile').getElementsByTagName('span');
121
for(var i =0;i<filenames.length;i++)
122
{
123
if( filenames[i].getAttribute('Num') == count)
124
{
125
var file = filenames[i].getElementsByTagName('input').item(0).value;
126
if(file == '')
127
{
128
alert('请选择文件');
129
return false;
130
}
131
if(CheckOption(file))
132
{
133
alert('文件已经在列表中存在。');
134
return false;
135
}
136
filenames[i].style.display='none';
137
appendOptionLast(file);
138
return true;
139
}
140
141
}
142
return true;
143
}
144
function CheckUpload()
145
{
146
var str = getOption();
147
if(str == '')
148
{
149
alert('请选择要上载的文件。');
150
return false;
151
}
152
document.all.hidvalue.value=str;
153
return true;
154
}
155
156
157
function insertOptionBefore(num)
158
{
159
var elSel = document.getElementById(SelectControlID);
160
if (elSel.selectedIndex >= 0) {
161
var elOptNew = document.createElement('option');
162
elOptNew.text = num;
163
elOptNew.value = num;
164
var elOptOld = elSel.options[elSel.selectedIndex];
165
try {
166
elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
167
}
168
catch(ex) {
169
elSel.add(elOptNew, elSel.selectedIndex); // IE only
170
}
171
}
172
}
173
174
function removeOptionSelected()
175
{
176
var elSel = document.getElementById(SelectControlID);
177
var i;
178
for (i = elSel.length - 1; i>=0; i--) {
179
if (elSel.options[i].selected) {
180
elSel.remove(i);
181
}
182
}
183
}
184
185
function appendOptionLast(num)
186
{
187
var elOptNew = document.createElement('option');
188
elOptNew.text = num;
189
elOptNew.value =num;
190
var elSel = document.getElementById(SelectControlID);
191
192
try {
193
elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
194
}
195
catch(ex) {
196
elSel.add(elOptNew); // IE only
197
}
198
}
199
200
function removeOptionLast()
201
{
202
var elSel = document.getElementById(SelectControlID);
203
if (elSel.length > 0)
204
{
205
elSel.remove(elSel.length - 1);
206
}
207
}
208
function getOption(split)
209
{
210
if(!split)
211
split = ',';
212
var elSel = document.getElementById(SelectControlID);
213
var i;
214
var str='';
215
for (i = elSel.length - 1; i>=0; i--) {
216
str+=elSel.options[i].value+split;
217
}
218
return str;
219
}
220
function CheckOption(value)
221
{
222
var elSel = document.getElementById(SelectControlID);
223
var i;
224
for (i = elSel.length - 1; i>=0; i--) {
225
if(elSel.options[i].value == value)
226
return true;
227
}
228
return false;
229
}
230
//-->
231
</script>";
232
#endregion
233
}
234
}
235
使用方法如下:
在页面中加入上面的控件,ID为:FileUpLoadEx1

使用方法code
1
if (FileUpLoadEx1.Count == 0)
2
{
3
InfoTips = "请选择要上传的文件.";
4
return;
5
}
6
HttpFileCollection files = HttpContext.Current.Request.Files;
7
bool IsError = false;
8
foreach (int i in FileUpLoadEx1.FileIndex)
9
{
10
try
11
{
12
uploadfile(files[i], folder);
13
InfoTips = string.Format("文件\"
{0}\"上载成功!", files[i].FileName);
14
}
15
catch (Exception ee)
16
{
17
IsError = true;
18
InfoTipsLog.Exception(string.Format("文件\"
{0}\"上载出错!错误:{1}", files[i].FileName, ee.Message));
19