Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件

翻译|其它|编辑:郝浩|2007-08-22 15:51:22.000|阅读 1258 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

1public partial class UpMultiFileControl2 : System.Web.UI.UserControl
  2
...{
  3    
protected void Page_Load(object sender, EventArgs e)
  4    ...
{
  5        
if (!Page.IsPostBack)
  6        ...
{
  7            SaveCurrentPageFileControls();
  8        }

  9    }

 10    
protected void btAddFile_Click(object sender, EventArgs e)
 11    ...
{
 12        AddOneFileControl();
 13    }

 14
 15    /**/
/**//**//// <summary>
 16    /**//// 添加一个上传文件控件
 17    /**//// </summary>
 18    private void AddOneFileControl()
 19    ...
{
 20        ArrayList al = 
new ArrayList();
 21        
this.tbFiles.Rows.Clear();
 22        GetFileControlsFromSession();        
 23        HtmlTableRow htr = 
new HtmlTableRow();
 24        HtmlTableCell htc = 
new HtmlTableCell();        
 25        htc.Controls.Add(
new FileUpload());
 26        htr.Controls.Add(htc);
 27        
this.tbFiles.Rows.Add(htr);
 28        SaveCurrentPageFileControls();
 29    }

 30
 31    /**/
/**//**//// <summary>
 32    /**//// 读取缓存中存储的上传文件控件集
 33    /**//// </summary>
 34    private void GetFileControlsFromSession()
 35    ...
{
 36        ArrayList al = 
new ArrayList();       
 37        
if (Session["FilesControls"] != null)
 38        ...
{
 39            al = (System.Collections.ArrayList)Session["FilesControls"];
 40            
for (int i = 0; i < al.Count; i++)
 41            ...
{
 42                HtmlTableRow htr1 = 
new HtmlTableRow();                
 43                HtmlTableCell htc1 = 
new HtmlTableCell();
 44                htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
 45                htr1.Controls.Add(htc1);
 46                
this.tbFiles.Rows.Add(htr1);
 47            }

 48        }

 49    }

 50    
 51    /**/
/**//**//// <summary>
 52    /**//// 保存当前页面上传文件控件集到缓存中
 53    /**//// </summary>    
 54    private void SaveCurrentPageFileControls()
 55    ...
{        
 56        ArrayList al = 
new ArrayList();        
 57        
foreach (Control controlTR in this.tbFiles.Controls)
 58        ...
{
 59            
if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
 60            ...
{
 61                HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];
 62                
foreach (Control controlFileUpload in htc.Controls)
 63                ...
{
 64                    
if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
 65                    ...
{
 66                        FileUpload tempFileUpload = (FileUpload)controlFileUpload;
 67                        al.Add(tempFileUpload);
 68                    }

 69                }

 70            }
            
 71        }
  
 72        Session.Add("FilesControls", al);
 73    }

 74
 75    
protected void btUpFiles_Click(object sender, EventArgs e)
 76    ...
{
 77        UpLoadFiles();
 78    }

 79
 80    /**/
/**//**//// <summary>
 81    /**//// 上传文件操作
 82    /**//// </summary>
 83    private void UpLoadFiles()
 84    ...
{
 85        
string filepath = Server.MapPath("./")+"UploadFiles";
 86        
 87        HttpFileCollection uploadedFiles = Request.Files;       
 88        
for (int i = 0; i < uploadedFiles.Count; i++)
 89        ...
{    
 90           HttpPostedFile userPostedFile = uploadedFiles[i];        
 91           
try
 92           ...
{    
 93              
if (userPostedFile.ContentLength > 0 )
 94              ...
{  
 95                 userPostedFile.SaveAs(filepath + "\" + System.IO.Path.GetFileName(userPostedFile.FileName));
 96                 Response.Write("
已上传文件: "" + filepath +"\"+ userPostedFile.FileName +""<br><br>" );                                   
 97              }
    
 98           }
 
 99           
catch
100           ...
{
101               Response.Write("
上传文件: "" + userPostedFile.FileName +""出错!");
102           }
    
103       }

104       
if (Session["FilesControls"] != null)
105       ...
{
106           Session.Remove("FilesControls");
107       }

108    }
    
109}
1
public partial class UpMultiFileControl2 : System.Web.UI.UserControl
  2
...{
  3    
protected void Page_Load(object sender, EventArgs e)
  4    ...
{
  5        
if (!Page.IsPostBack)
  6        ...
{
  7            SaveCurrentPageFileControls();
  8        }

  9    }

 10    
protected void btAddFile_Click(object sender, EventArgs e)
 11    ...
{
 12        AddOneFileControl();
 13    }

 14
 15    /**/
/**//**//// <summary>
 16    /**//// 添加一个上传文件控件
 17    /**//// </summary>
 18    private void AddOneFileControl()
 19    ...
{
 20        ArrayList al = 
new ArrayList();
 21        
this.tbFiles.Rows.Clear();
 22        GetFileControlsFromSession();        
 23        HtmlTableRow htr = 
new HtmlTableRow();
 24        HtmlTableCell htc = 
new HtmlTableCell();        
 25        htc.Controls.Add(
new FileUpload());
 26        htr.Controls.Add(htc);
 27        
this.tbFiles.Rows.Add(htr);
 28        SaveCurrentPageFileControls();
 29    }

 30
 31    /**/
/**//**//// <summary>
 32    /**//// 读取缓存中存储的上传文件控件集
 33    /**//// </summary>
 34    private void GetFileControlsFromSession()
 35    ...
{
 36        ArrayList al = 
new ArrayList();       
 37        
if (Session["FilesControls"] != null)
 38        ...
{
 39            al = (System.Collections.ArrayList)Session["FilesControls"];
 40            
for (int i = 0; i < al.Count; i++)
 41            ...
{
 42                HtmlTableRow htr1 = 
new HtmlTableRow();                
 43                HtmlTableCell htc1 = 
new HtmlTableCell();
 44                htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
 45                htr1.Controls.Add(htc1);
 46                
this.tbFiles.Rows.Add(htr1);
 47            }

 48        }

 49    }

 50    
 51    /**/
/**//**//// <summary>
 52    /**//// 保存当前页面上传文件控件集到缓存中
 53    /**//// </summary>    
 54    private void SaveCurrentPageFileControls()
 55    ...
{        
 56        ArrayList al = 
new ArrayList();        
 57        
foreach (Control controlTR in this.tbFiles.Controls)
 58        ...
{
 59            
if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
 60            ...
{
 61                HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];
 62                
foreach (Control controlFileUpload in htc.Controls)
 63                ...
{
 64                    
if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
 65                    ...
{
 66                        FileUpload tempFileUpload = (FileUpload)controlFileUpload;
 67                        al.Add(tempFileUpload);
 68                    }

 69                }

 70            }
            
 71        }
  
 72        Session.Add("FilesControls", al);
 73    }

 74
 75    
protected void btUpFiles_Click(object sender, EventArgs e)
 76    ...
{
 77        UpLoadFiles();
 78    }

 79
 80    /**/
/**//**//// <summary>
 81    /**//// 上传文件操作
 82    /**//// </summary>
 83    private void UpLoadFiles()
 84    ...
{
 85        
string filepath = Server.MapPath("./")+"UploadFiles";
 86        
 87        HttpFileCollection uploadedFiles = Request.Files;       
 88        
for (int i = 0; i < uploadedFiles.Count; i++)
 89        ...
{    
 90           HttpPostedFile userPostedFile = uploadedFiles[i];        
 91           
try
 92           ...
{    
 93              
if (userPostedFile.ContentLength > 0 )
 94              ...
{  
 95                 userPostedFile.SaveAs(filepath + "\" + System.IO.Path.GetFileName(userPostedFile.FileName));
 96                 Response.Write("
已上传文件: "" + filepath +"\"+ userPostedFile.FileName +""<br><br>" );                                   
 97              }
    
 98           }
 
 99           
catch
100           ...
{
101               Response.Write("
上传文件: "" + userPostedFile.FileName +""出错!");
102           }
    
103       }

104       
if (Session["FilesControls"] != null)
105       ...
{
106           Session.Remove("FilesControls");
107       }

108    }
    
109}

改变上传文件大小和时间限制

       <httpRuntime>
            executionTimeout="110"              //
允许上传文件最大等待时间
            maxRequestLength="4096"        //
上传文件大小,默认为4M
       </httpRuntime>

       上传文件大小是由上面两个参数所决定的涉及到安全因素,最好不要设得太大.  


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP