狂野都城

一壶浊酒喜相逢,古今多少事, 都付笑谈中。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# 同时上传多个文件!

Posted on 2005-12-27 10:47  狂野都城  阅读(2289)  评论(0编辑  收藏  举报

 

<TABLE class="tabcolor0" cellSpacing="1" cellPadding="3" width="98%" border="0"> 
            
<TR> 
                
<TD class="tabcolor2" style="WIDTH: 132px"> 
                    
<DIV align="right">上传文件</DIV> 
                
</TD> 
                
<TD class="tabcolor3">输入文件个数(小于5个): 
                    
<asp:TextBox id="txtNum" runat="server" Width="40px">0</asp:TextBox> 
                    
<asp:Button id="Button1" runat="server" Text="确定" CausesValidation="False"></asp:Button></TD> 
            
</TR> 
            
<TR> 
                
<TD class="tabcolor2" style="WIDTH: 132px"> 
                    
<DIV align="right">&nbsp;</DIV> 
                
</TD> 
                
<TD class="tabcolor3"> 
                    
<asp:Repeater id="Repeater1" runat="server"> 
                        
<ItemTemplate> 
                            文件标题: 
                            
<asp:TextBox id="fileTitle" runat="server"></asp:TextBox> 
                            
<input id="upfile" type="file" name="file" runat="server"><br /> 
                        
</ItemTemplate> 
                    
</asp:Repeater> 
                
</TD> 
            
</TR> 
                        
<TR>
                            
<TD class="tabcolor3" align="center" colSpan="2"><asp:button id="btnAdd" runat="server" Text="增 加"></asp:button></TD>
                        
</TR>
</TABLE> 

在你的ASPX 页面把上面的代码放在
<FORM id="Form1" method="post" runat="server"> 和 </FORM>之间


然后在你的代码里面放入下面代码即可同时上传多个图片!

private void Button1_Click(object sender, System.EventArgs e)
        {
            IList list = new ArrayList();
            for(int i = 0; i 
< Int32.Parse(this.txtNum.Text); i++)
            {
                list.Add(1);
            }
            Repeater1.DataSource 
= list;
            
this.Repeater1.DataBind();
        }

private void btnAdd_Click(object sender, System.EventArgs e)
{
                int filenum 
= Int32.Parse(this.txtNum.Text);    
                
string strfilename = "";//返回: 标题|文件名;标题|文件名;标题|文件名
                if(filenum 
> 0)
                {
                    System.Web.UI.HtmlControls.HtmlInputFile[] f = new HtmlInputFile[filenum];
                    System.Web.UI.WebControls.TextBox[] myTitle = new TextBox[filenum];

                    for( int i=0;i 
< filenum;i++)
                    {
                        f[i] 
= (HtmlInputFile)(this.Repeater1.Controls[i].FindControl("upfile"));
                        
myTitle[i] = (TextBox)(this.Repeater1.Controls[i].FindControl("fileTitle"));
                        

                        
if(f[i].PostedFile.FileName != "" && myTitle[i].Text != "")
                        {
                            //开始上传
                            string[] filetype 
= new string[]{"jpg","gif","png","JPG","GIF","PNG"};
                            
                            if(strfilename 
== null || strfilename =="")
                            
{
                                strfilename 
=  myTitle[i].Text + "|" + new Upload().UploadFile(path , 204800, filetype, f[i]);
                            }
                            else
                            {
                                strfilename +
= ";"+  myTitle[i].Text + "|" +  new Upload().UploadFile(path , 204800, filetype, f[i]);
                            }

                            /*
                            Response.Write(i + ":");
                            Response .Write(myTitle[i].Text + "|");
                            Response.Write(f[i].PostedFile.FileName +"<br
/>");
                            */
                        }
                    }
               }
}


注意:本程序使用了一个上传类.
http://joejoe.cnblogs.com/archive/2005/12/21/301581.aspx