Asp.net(C#)多文件上传

FileUp.aspx 页面

 1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="FileUp.aspx.cs" AutoEventWireup="false" Inherits="TestCenter.FileUp"  %>  
 2 None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >  
 3 None.gif < HTML >  
 4 None.gif   < HEAD >  
 5 None.gif     < title > 多文件上传 </ title >  
 6 ExpandedBlockStart.gifContractedBlock.gif     < script  language ="JavaScript" > dot.gif  
 7InBlock.gif    function addFile() 
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
 9InBlock.gif     var str = '<INPUT type="file" size="50" NAME="File">
10InBlock.gif     document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str) 
11ExpandedSubBlockEnd.gif    }
 
12ExpandedBlockEnd.gif    
</ script >  
13 None.gif   </ HEAD >  
14 None.gif   < body >  
15 None.gif     < form  id ="form1"  method ="post"  runat ="server"  enctype ="multipart/form-data" >  
16 None.gif       < div  align ="center" >  
17 None.gif         < h3 > 多文件上传 </ h3 >  
18 None.gif         < id ="MyFile" >< INPUT  type ="file"  size ="50"  NAME ="File" ></ P >  
19 None.gif         < P >  
20 None.gif           < input  type ="button"  value ="增加(Add)"  onclick ="addFile()" >  
21 None.gif           < input  onclick ="this.form.reset()"  type ="button"  value ="重置(ReSet)" >  
22 None.gif           < asp:Button  Runat ="server"  Text ="开始上传"  ID ="UploadButton" ></ asp:Button >  
23 None.gif         </ P >  
24 None.gif         < P >  
25 None.gif         < asp:Label  id ="strStatus"  runat ="server"  Font-Names ="宋体"  Font-Bold ="True"  Font-Size ="9pt"   
26 None.gif          Width ="500px"  BorderStyle ="None"  BorderColor ="White" ></ asp:Label >  
27 None.gif         </ P >   
28 None.gif       </ div >  
29 None.gif     </ form >  
30 None.gif   </ body >  
31 None.gif </ HTML >  
32 None.gif


//****************************************************************
//FileUp.aspx.cs

 1 None.gif using  System; 
 2 None.gif using  System.Collections; 
 3 None.gif using  System.ComponentModel; 
 4 None.gif using  System.Data; 
 5 None.gif using  System.Drawing; 
 6 None.gif using  System.Web; 
 7 None.gif using  System.Web.SessionState; 
 8 None.gif using  System.Web.UI; 
 9 None.gif using  System.Web.UI.WebControls; 
10 None.gif using  System.Web.UI.HtmlControls; 
11 None.gif
12 None.gif namespace  TestCenter
13 ExpandedBlockStart.gifContractedBlock.gif dot.gif
14ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>   
15InBlock.gif  /// 实现多文件上传 
16ExpandedSubBlockEnd.gif  /// </summary> 

17InBlock.gif  public class FileUp: System.Web.UI.Page 
18ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif
19InBlock.gif    protected System.Web.UI.WebControls.Button UploadButton; 
20InBlock.gif    protected System.Web.UI.WebControls.Label strStatus; 
21InBlock.gif
22InBlock.gif    private void Page_Load(object sender, System.EventArgs e) 
23ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
24InBlock.gif            if (this.IsPostBack) this.SaveImages(); 
25ExpandedSubBlockEnd.gif    }
 
26InBlock.gif
27InBlock.gif    private Boolean SaveImages() 
28ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
29ExpandedSubBlockStart.gifContractedSubBlock.gif      /**////'遍历File表单元素 
30InBlock.gif      HttpFileCollection files  = HttpContext.Current.Request.Files; 
31InBlock.gif
32ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//// '状态信息 
33InBlock.gif      System.Text.StringBuilder strMsg = new System.Text.StringBuilder(); 
34InBlock.gif      strMsg.Append("上传的文件分别是:<hr color=red>"); 
35InBlock.gif      try 
36ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif
37InBlock.gif        for(int iFile = 0; iFile < files.Count; iFile++
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
39ExpandedSubBlockStart.gifContractedSubBlock.gif          /**////'检查文件扩展名字 
40InBlock.gif          HttpPostedFile postedFile = files[iFile]; 
41InBlock.gif          string fileName, fileExtension; 
42InBlock.gif          fileName = System.IO.Path.GetFileName(postedFile.FileName); 
43InBlock.gif          if (fileName != ""
44ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif
45InBlock.gif            fileExtension = System.IO.Path.GetExtension(fileName); 
46InBlock.gif            strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>"); 
47InBlock.gif            strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>"); 
48InBlock.gif            strMsg.Append("上传文件的文件名:" + fileName + "<br>"); 
49InBlock.gif            strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>"); 
50ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////'可根据扩展名字的不同保存到不同的文件夹 
51ExpandedSubBlockEnd.gif            ///注意:可能要修改你的文件夹的匿名写入权限。 

52InBlock.gif            postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/"+ fileName); 
53ExpandedSubBlockEnd.gif          }
 
54ExpandedSubBlockEnd.gif        }
 
55InBlock.gif        strStatus.Text = strMsg.ToString(); 
56InBlock.gif        return true
57ExpandedSubBlockEnd.gif      }
 
58InBlock.gif      catch(System.Exception Ex) 
59ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif
60InBlock.gif        strStatus.Text = Ex.Message; 
61InBlock.gif        return false
62ExpandedSubBlockEnd.gif      }
 
63ExpandedSubBlockEnd.gif    }
 
64InBlock.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值