Asp.net 2.0 用FileUpload 控件实现多文件上传用户控件(示例代码下载)

翻译|其它|编辑:郝浩|2006-05-08 09:51:00.000|阅读 4072 次

概述:

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


(一). 示例代码

public partial class UpMultiFileControl2 : System.Web.UI.UserControl
{
 protected void Page_Load(object sender, EventArgs e)
 {
   if (!Page.IsPostBack)
    {
      SaveCurrentPageFileControls();
    }
 }
 protected void btAddFile_Click(object sender, EventArgs e)
 {
    AddOneFileControl();
 }

 /**//// <summary>
 /// 添加一个上传文件控件
 /// </summary>

 private void AddOneFileControl()
 {
     ArrayList al = new ArrayList();
     this.tbFiles.Rows.Clear();
     GetFileControlsFromSession();
     HtmlTableRow htr = new HtmlTableRow();
     HtmlTableCell htc = new HtmlTableCell();
     htc.Controls.Add(new FileUpload());
     htr.Controls.Add(htc);
     this.tbFiles.Rows.Add(htr);
     SaveCurrentPageFileControls();
 }

 /**//// <summary>
 /// 读取缓存中存储的上传文件控件集
 /// </summary>

 private void GetFileControlsFromSession()
 {
   ArrayList al = new ArrayList();
   if (Session["FilesControls"] != null)
    {
      al = (System.Collections.ArrayList)Session["FilesControls"];
      for (int i = 0; i < al.Count; i++)
       {
         HtmlTableRow htr1 = new HtmlTableRow();
         HtmlTableCell htc1 = new HtmlTableCell();
         htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
         htr1.Controls.Add(htc1);
         this.tbFiles.Rows.Add(htr1);
       }
    }
 }
 
 /**//// <summary>
 /// 保存当前页面上传文件控件集到缓存中
 /// </summary>

 private void SaveCurrentPageFileControls()
 {
    ArrayList al = new ArrayList();
    foreach (Control controlTR in this.tbFiles.Controls)
     {
       if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
        {
          HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];
          foreach (Control controlFileUpload in htc.Controls)
           {
             if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
           {
             FileUpload tempFileUpload = (FileUpload)controlFileUpload;
             al.Add(tempFileUpload);
            }
         }
        }
     }
   Session.Add("FilesControls", al);
 }

 protected void btUpFiles_Click(object sender, EventArgs e)
 {
    UpLoadFiles();
 }

   /**//// <summary>
  /// 上传文件操作
  /// </summary>

 private void UpLoadFiles()
 {
    string filepath = Server.MapPath("./")+"UploadFiles";
 
    HttpFileCollection uploadedFiles = Request.Files;
    for (int i = 0; i < uploadedFiles.Count; i++)
     {
        HttpPostedFile userPostedFile = uploadedFiles[i];
    try
     {
        if (userPostedFile.ContentLength > 0 )
        {
           userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));
           Response.Write("已上传文件: \"" + filepath +"\\"+ userPostedFile.FileName +"\"<br><br>" );
        }
    }
 catch
   {
      Response.Write("上传文件: \"" + userPostedFile.FileName +"\"出错!");
   }
}
 if (Session["FilesControls"] != null)
  {
     Session.Remove("FilesControls");
    }
  }
}


(二). 改变上传文件大小和时间限制


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

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

(三). 示例源代码下载


http://www.cnblogs.com/Files/ChengKing/UpMultiFileControl.rar

 


标签:

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


为你推荐

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


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP