在上篇asp .net 多文档上传控件实现的多文件上载控件,在上载文件太大的时候,就会出现“文件已经关闭”的错误,现在这个版本,重新实现修改原来的方式,可以上载任意大小的文件,并且在添加文件时也不用回调服务端,删除文件时,也会动态的删除选择的文件,截图如下:
选择文件:
上载成功:
源码
{
16publicclassMutiLoad:WebControl
17{
18stringstrFiles="";
19List<int>list=newList<int>();
20
21publicintCount
22{
23get24{
25
26returnlist.Count;
27}28}29publicint[]FileIndex
30{
31get32{
33returnlist.ToArray();
34}35}36protectedoverridevoidOnLoad(EventArgse)
37{
38base.OnLoad(e);
39if(Page.IsPostBack)
40{
41if(Page.Request.Form["hidvalue"]==null)
42strFiles="";
43else44strFiles=","+Page.Request.Form["hidvalue"].Trim(',')+",";
45HttpFileCollectionfiles1=HttpContext.Current.Request.Files;
46for(inti=0;i<files1.Count;i++)
47{
48if(CheckFile(files1[i].FileName))
49list.Add(i);
50}51
52}53}54privateboolCheckFile(stringFileName)
55{
56if(strFiles.IndexOf(","+FileName+",")==-1)
57returnfalse;
58strFiles=strFiles.Replace(FileName+",","");
59returntrue;
60}61protectedoverridevoidOnPreRender(EventArgse)
62{
63if(!Page.ClientScript.IsClientScriptBlockRegistered("MyJavaScript"))
64{
65Page.ClientScript.RegisterClientScriptBlock
66(Page.GetType(),"MyJavaScript",strScript);
67}68base.OnPreRender(e);
69HtmlFormform=this.Page.Form;
70if((form!=null)&&(form.Enctype.Length==0))
71{
72form.Enctype="multipart/form-data";
73}74}75protectedoverridevoidRender(HtmlTextWriterwriter)
76{
77base.Render(writer);
78writer.Write("<inputtype='hidden'id='hidvalue'name='hidvalue'/>");
79writer.Write(strTable);
80}81
82WriteTable#regionWriteTable83privatereadonlystringstrTable=@"<tableborder='0'cellpadding='0'cellspacing='0'width='100%'>
84<tr>
85<td>
86<pid='MyFile'>
87<spannum='1'>
88<inputtype='file'size='60'name='File'style='100%;height:25px'/></span></p>
89</td>
90<tdalign='left'style='100px'>
91 <inputtype='button'value='增加'onclick='addFile()'/>
92<inputtype='button'value='删除'
93onclick='javascript:removeOptionSelected()'/>
94</td>
95</tr>
96<tr>
97<tdcolspan='2'align='left'>
98<selectsize='10'name='SelectListBox'id='SelectListBox'style='100%'>
99</select>
100</td>
101</tr>
102</table>";
103#endregion104script#regionscript105privatereadonlystringstrScript=@"<scriptlanguage='JavaScript'type='text/javascript'>
106<!--
107varcount=1;
108varSelectControlID='SelectListBox';
109functionaddFile()
110{
111
112if(!CheckFile())
113return;
114count++;
115varstr=""<spanNum='""+count+""'><INPUTtype='file'size='60'NAME='File' /></span>"";
116document.getElementById('MyFile').insertAdjacentHTML('beforeEnd',str)
117}
118functionCheckFile()
119{
120varfilenames=document.getElementById('MyFile').getElementsByTagName('span');
121for(vari=0;i<filenames.length;i++)
122{
123if(filenames[i].getAttribute('Num')==count)
124{
125varfile=filenames[i].getElementsByTagName('input').item(0).value;
126if(file=='')
127{
128alert('请选择文件');
129returnfalse;
130}
131if(CheckOption(file))
132{
133alert('文件已经在列表中存在。');
134returnfalse;
135}
136filenames[i].style.display='none';
137appendOptionLast(file);
138returntrue;
139}
140
141}
142returntrue;
143}
144functionCheckUpload()
145{
146varstr=getOption();
147if(str=='')
148{
149alert('请选择要上载的文件。');
150returnfalse;
151}
152document.all.hidvalue.value=str;
153returntrue;
154}
155
156
157functioninsertOptionBefore(num)
158{
159varelSel=document.getElementById(SelectControlID);
160if(elSel.selectedIndex>=0){
161varelOptNew=document.createElement('option');
162elOptNew.text=num;
163elOptNew.value=num;
164varelOptOld=elSel.options[elSel.selectedIndex];
165try{
166elSel.add(elOptNew,elOptOld);//standardscompliant;doesn'tworkinIE
167}
168catch(ex){
169elSel.add(elOptNew,elSel.selectedIndex);//IEonly
170}
171}
172}
173
174functionremoveOptionSelected()
175{
176varelSel=document.getElementById(SelectControlID);
177vari;
178for(i=elSel.length-1;i>=0;i--){
179if(elSel.options[i].selected){
180elSel.remove(i);
181}
182}
183}
184
185functionappendOptionLast(num)
186{
187varelOptNew=document.createElement('option');
188elOptNew.text=num;
189elOptNew.value=num;
190varelSel=document.getElementById(SelectControlID);
191
192try{
193elSel.add(elOptNew,null);//standardscompliant;doesn'tworkinIE
194}
195catch(ex){
196elSel.add(elOptNew);//IEonly
197}
198}
199
200functionremoveOptionLast()
201{
202varelSel=document.getElementById(SelectControlID);
203if(elSel.length>0)
204{
205elSel.remove(elSel.length-1);
206}
207}
208functiongetOption(split)
209{
210if(!split)
211split=',';
212varelSel=document.getElementById(SelectControlID);
213vari;
214varstr='';
215for(i=elSel.length-1;i>=0;i--){
216str+=elSel.options[i].value+split;
217}
218returnstr;
219}
220functionCheckOption(value)
221{
222varelSel=document.getElementById(SelectControlID);
223vari;
224for(i=elSel.length-1;i>=0;i--){
225if(elSel.options[i].value==value)
226returntrue;
227}
228returnfalse;
229}
230//-->
231</script>";
232#endregion233}234}235
使用方法如下:
在页面中加入上面的控件,ID为:FileUpLoadEx1
10try11{
12uploadfile(files[i],folder);
13InfoTips=string.Format("文件\"{0}\"上载成功!",files[i].FileName);
14}15catch(Exceptionee)
16{
17IsError=true;
18InfoTipsLog.Exception(string.Format("文件\"{0}\"上载出错!错误:{1}",files[i].FileName,ee.Message));
19logInfo.Exception(ee);
20}21}22if(!IsError)
23Return("上载成功.");