带进度条的文件上传控件使用小结

本文介绍了一种使用ASP.NET进行文件上传的方法,包括单个文件和批量文件上传的实现过程。通过将ASP.NET按钮控件替换为HTML按钮控件来确保更好的兼容性和用户体验。

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

一、上传界面内容

选择文件:


   
选择文件:
   
 

说明:为了能正确显示,把原有asp.NET按钮控件换成了HTML的按钮控件

二、后台代码

       1.page_load部分内容

            private void Page_Load(object sender, System.EventArgs e)
             {
                      // 在此处放置用户代码以初始化页面
                      UpLoadHelper helper = new UpLoadHelper();   
                      helper.RegisterProgressBar(bt_save.UniqueID);
                      string path = Path.Combine(Server.MapPath("."),"loadfile");
                      if(!Directory.Exists(path))
                       {
                          Directory.CreateDirectory(path);
                      }
                       helper.UploadFolder = path;  
                      bind();
             }

     2.加入了自定义的显示上传文件夹的文件列表方法

           public void bind()
           {
                  string path = Path.Combine(Server.MapPath("."),"loadfile");
                  if(!Directory.Exists(path))
                   {
                         Directory.CreateDirectory(path);
                   }
                  Table1.Rows.Clear();
                  foreach(string filename in Directory.GetFiles(path))
                  {
                         TableRow r = new TableRow();
                         TableCell c = new TableCell();
                         c.Text = filename;
                         r.Cells.Add(c);
                        Table1.Rows.Add(r);
                }
          }

    3.批量上传文件的代码

     UpLoadHelper helper = new UpLoadHelper();
    
    string path = Path.Combine(Server.MapPath("."),"loadfile");
       
    foreach(UpLoadFile file in helper.GetUploadFileList("file1"))
    {
     file.SaveAs(Path.Combine(path,Path.GetFileName(file.FileName)));     
    }
    bind();

4.单个文件上传的代码

UpLoadHelper helper = new UpLoadHelper();
   
   string path = Path.Combine(Server.MapPath("."),"loadfile");
   UpLoadFile file = helper.GetUploadFile("s_file");
   file.SaveAs(Path.Combine(path,Path.GetFileName(file.FileName))); 
   bind(); 

三、说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值