页面文件
代码
1
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
Default
"
%>
2
3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head id ="Head1" runat ="server" >
7 < title > 多文件上传 </ title >
8
9 < script type ="text/javascript" >
10 function addFile() {
11 var div = document.createElement( " div " );
12 var f = document.createElement( " input " );
13 f.setAttribute( " type " , " file " )
14 f.setAttribute( " name " , " File " )
15 f.setAttribute( " size " , " 50 " )
16 div.appendChild(f)
17 var d = document.createElement( " input " );
18 d.setAttribute( " type " , " button " )
19 d.setAttribute( " onclick " , " deteFile(this) " );
20 d.setAttribute( " value " , " 移除 " )
21 div.appendChild(d)
22 document.getElementById( " _container " ).appendChild(div);
23 }
24
25 function deteFile(o) {
26 while (o.tagName != " DIV " ) o = o.parentNode;
27 o.parentNode.removeChild(o);
28 }
29 </ script >
30
31 </ head >
32 < body >
33 < form id ="form1" runat ="server" method ="post" enctype ="multipart/form-data" >
34 < h3 >
35 多文件上传 </ h3 >
36
37 < div id ="_container" >
38 < input type ="file" size ="50" name ="File" />
39 </ div >
40 < div >
41 < input type ="button" value ="添加文件(Add)" onclick ="addFile()" id ="Button1" />
42 </ div >
43 < div style ="padding: 10px 0" >
44 < asp:Button runat ="server" Text ="开始上传" ID ="UploadButton" OnClick ="UploadButton_Click" >
45 </ asp:Button >
46 </ div >
47 < div >
48 < asp:Label ID ="strStatus" runat ="server" Font-Names ="宋体" Font-Bold ="True" Font-Size ="9pt"
49 Width ="500px" BorderStyle ="None" BorderColor ="White" ></ asp:Label >
50 </ div >
51 </ form >
52 </ body >
53 </ html >
54
2
3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head id ="Head1" runat ="server" >
7 < title > 多文件上传 </ title >
8
9 < script type ="text/javascript" >
10 function addFile() {
11 var div = document.createElement( " div " );
12 var f = document.createElement( " input " );
13 f.setAttribute( " type " , " file " )
14 f.setAttribute( " name " , " File " )
15 f.setAttribute( " size " , " 50 " )
16 div.appendChild(f)
17 var d = document.createElement( " input " );
18 d.setAttribute( " type " , " button " )
19 d.setAttribute( " onclick " , " deteFile(this) " );
20 d.setAttribute( " value " , " 移除 " )
21 div.appendChild(d)
22 document.getElementById( " _container " ).appendChild(div);
23 }
24
25 function deteFile(o) {
26 while (o.tagName != " DIV " ) o = o.parentNode;
27 o.parentNode.removeChild(o);
28 }
29 </ script >
30
31 </ head >
32 < body >
33 < form id ="form1" runat ="server" method ="post" enctype ="multipart/form-data" >
34 < h3 >
35 多文件上传 </ h3 >
36
37 < div id ="_container" >
38 < input type ="file" size ="50" name ="File" />
39 </ div >
40 < div >
41 < input type ="button" value ="添加文件(Add)" onclick ="addFile()" id ="Button1" />
42 </ div >
43 < div style ="padding: 10px 0" >
44 < asp:Button runat ="server" Text ="开始上传" ID ="UploadButton" OnClick ="UploadButton_Click" >
45 </ asp:Button >
46 </ div >
47 < div >
48 < asp:Label ID ="strStatus" runat ="server" Font-Names ="宋体" Font-Bold ="True" Font-Size ="9pt"
49 Width ="500px" BorderStyle ="None" BorderColor ="White" ></ asp:Label >
50 </ div >
51 </ form >
52 </ body >
53 </ html >
54
后台代码
后台代码
1
using
System;
2 using System.Collections.Generic;
3
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 public partial class Default : System.Web.UI.Page
9 {
10 protected void UploadButton_Click( object sender, EventArgs e)
11 {
12 /// '遍历File表单元素
13 HttpFileCollection files = HttpContext.Current.Request.Files;
14
15 /// '状态信息
16 System.Text.StringBuilder strMsg = new System.Text.StringBuilder( "" );
17 strMsg.Append( " 上传的文件分别是:<hr color='red'/> " );
18 try
19 {
20 for ( int iFile = 0 ; iFile < files.Count; iFile ++ )
21 {
22 /// '检查文件扩展名字
23 HttpPostedFile postedFile = files[iFile];
24 string fileName, fileExtension;
25 fileName = System.IO.Path.GetFileName(postedFile.FileName);
26 if (fileName != "" )
27 {
28 fileExtension = System.IO.Path.GetExtension(fileName);
29 strMsg.Append( " 上传的文件类型: " + postedFile.ContentType.ToString() + " <br> " );
30 strMsg.Append( " 客户端文件地址: " + postedFile.FileName + " <br> " );
31 strMsg.Append( " 上传文件的文件名: " + fileName + " <br> " );
32 strMsg.Append( " 上传文件的扩展名: " + fileExtension + " <br><hr> " );
33 /// '可根据扩展名字的不同保存到不同的文件夹
34 /// 注意:可能要修改你的文件夹的匿名写入权限。
35 postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath( " images/ " ) + fileName);
36 }
37 }
38 strStatus.Text = strMsg.ToString();
39 }
40 catch (System.Exception Ex)
41 {
42 strStatus.Text = Ex.Message;
43 }
44
45 }
46 }
47
2 using System.Collections.Generic;
3
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 public partial class Default : System.Web.UI.Page
9 {
10 protected void UploadButton_Click( object sender, EventArgs e)
11 {
12 /// '遍历File表单元素
13 HttpFileCollection files = HttpContext.Current.Request.Files;
14
15 /// '状态信息
16 System.Text.StringBuilder strMsg = new System.Text.StringBuilder( "" );
17 strMsg.Append( " 上传的文件分别是:<hr color='red'/> " );
18 try
19 {
20 for ( int iFile = 0 ; iFile < files.Count; iFile ++ )
21 {
22 /// '检查文件扩展名字
23 HttpPostedFile postedFile = files[iFile];
24 string fileName, fileExtension;
25 fileName = System.IO.Path.GetFileName(postedFile.FileName);
26 if (fileName != "" )
27 {
28 fileExtension = System.IO.Path.GetExtension(fileName);
29 strMsg.Append( " 上传的文件类型: " + postedFile.ContentType.ToString() + " <br> " );
30 strMsg.Append( " 客户端文件地址: " + postedFile.FileName + " <br> " );
31 strMsg.Append( " 上传文件的文件名: " + fileName + " <br> " );
32 strMsg.Append( " 上传文件的扩展名: " + fileExtension + " <br><hr> " );
33 /// '可根据扩展名字的不同保存到不同的文件夹
34 /// 注意:可能要修改你的文件夹的匿名写入权限。
35 postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath( " images/ " ) + fileName);
36 }
37 }
38 strStatus.Text = strMsg.ToString();
39 }
40 catch (System.Exception Ex)
41 {
42 strStatus.Text = Ex.Message;
43 }
44
45 }
46 }
47