Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)

本文分享了一个基于Asp.net 2.0的简单文件下载程序,该程序支持多线程和断点续传功能。虽然仅限于单个文件下载任务,但提供了核心代码和示例下载链接,可作为实现多任务下载的基础。文章还提及代码来源于个人兴趣项目,并非公司所有。
摘要由CSDN通过智能技术生成

(一) . 概述

        最近做了个C/S文件下载工具, 支持多任务, 多线程和断点续传功能. 其中部分代码是从网上找来的, 自己改了

许多Thread Bug, 并增加多任务, 断点续传等功能.  

        由于公司具有代码所有权, 不能将源代码共享.  自己对比较Asp.net感兴趣, 业余时间自己做了个简单的, 基于

Asp.net 2.0的, 目前能够执行对一个文件的下载任务, 但已经实现了多线程, 断点续传功能.  根据需要您可以增加

多任务 功能, 分享一下, 互相学习!  互相借鉴!

       时间仓促, 此程序还没有做很多参数方面的优化. 可以作参考用.

              

(二).运行效果

 

(三). 代码

    1. 核心 DownLoadState.cs 文件代码

   1  ///   <summary>
   2  ///  Author: [ ChengKing(ZhengJian) ] 
   3  ///  Blog:   Http://blog.csdn.net/ChengKing
   4  ///  注:从网上找了个优秀代码
   5  ///  扩展如下功能: 
   6  ///    1. 解决一些线程相关的Bug; 
   7  ///    2.扩展用控制文件实现断点续传功能.
   8  ///   </summary>
   9  namespace  DownLoadComponent
  10  {
  11       ///   <summary>
  12       ///  多线程辅助类(Add by ChengKing)
  13       ///   </summary>
  14       public   class  Task
  15      {
  16           string  _FromFileName;
  17           string  _ToFileName;
  18           int  _ThreadNum;
  19 
  20           public  Task( string  FromFileName,  string  ToFileName,  int  ThreadNum)
  21          {
  22               this ._FromFileName  =  FromFileName;
  23               this ._ToFileName  =  ToFileName;
  24               this ._ThreadNum  =  ThreadNum;
  25          }
  26 
  27           public   string  FromFileName
  28          {
  29               get
  30              {
  31                   return  _FromFileName;
  32              }
  33               set
  34              {
  35                  _FromFileName  =  value;
  36              }
  37          }
  38           public   string  ToFileName
  39          {
  40               get
  41              {
  42                   return  _ToFileName;
  43              }
  44               set
  45              {
  46                  _ToFileName  =  value;
  47              }
  48          }
  49           public   int  ThreadNum
  50          {
  51               get
  52              {
  53                   return  _ThreadNum;
  54              }
  55               set
  56              {
  57                  _ThreadNum  =  value;
  58              }
  59          }
  60      }
  61 
  62       ///   <summary>
  63       ///  记录下载的字节位置
  64       ///   </summary>
  65       public   class  DownLoadState
  66      {
  67           private   string  _FileName;
  68 
  69           private   string  _AttachmentName;
  70           private   int  _Position;
  71           private   string  _RequestURL;
  72           private   string  _ResponseURL;
  73           private   int  _Length;
  74 
  75           private   byte [] _Data;
  76 
  77           public   string  FileName
  78          {
  79               get
  80              {
  81                   return  _FileName;
  82              }
  83          }
  84 
  85           public   int  Position
  86          {
  87               get
  88              {
  89                   return  _Position;
  90              }
  91          }
  92 
  93           public   int  Length
  94          {
  95               get
  96              {
  97                   return  _Length;
  98              }
  99          }
 100 
 101 
 102           public   string  AttachmentName
 103          {
 104               get
 105              {
 106                   return  _AttachmentName;
 107              }
 108          }
 109 
 110           public   string  RequestURL
 111          {
 112               get
 113              {
 114                   return  _RequestURL;
 115              }
 116          }
 117 
 118           public   string  ResponseURL
 119          {
 120               get
 121              {
 122                   return  _ResponseURL;
 123              }
 124          }
 125 
 126 
 127           public   byte [] Data
 128          {
 129               get
 130              {
 131                   return  _Data;
 132              }
 133          }
 134 
 135           internal  DownLoadState( string  RequestURL,  string  ResponseURL,  string  FileName,  string  AttachmentName,  int  Position,  int  Length,  byte [] Data)
 136          {
 137               this ._FileName  =  FileName;
 138               this ._RequestURL  =  RequestURL;
 139               this ._ResponseURL  =  ResponseURL;
 140               this ._AttachmentName  =  AttachmentName;
 141               this ._Position  =  Position;
 142               this ._Data  =  Data;
 143               this ._Length  =  Length;
 144          }
 145 
 146           internal  DownLoadState( string  RequestURL,  string  ResponseURL,  string  FileName,  string  AttachmentName,  int  Position,  int  Length, ThreadCallbackHandler tch)
 147          {
 148               this ._RequestURL  =  RequestURL;
 149               this ._ResponseURL  =  ResponseURL;
 150               this ._FileName  =  FileName;
 151               this ._AttachmentName  =  AttachmentName;
 152               this ._Position  =  Position;
 153               this ._Length  =  Length;
 154               this ._ThreadCallback  =  tch;
 155          }
 156 
 157           internal  DownLoadState( string  RequestURL,  string  ResponseURL,  string  FileName,  string  AttachmentName,  int  Position,  int  Length)
 158          {
 159               this ._RequestURL  =  RequestURL;
 160               this ._ResponseURL  =  ResponseURL;
 161               this ._FileName  =  FileName;
 162               this ._AttachmentName  =  AttachmentName;
 163               this ._Position  =  Position;
 164               this ._Length  =  Length;
 165          }
 166 
 167           private  ThreadCallbackHandler _ThreadCallback;
 168 
 169           //
 170           internal   void  StartDownloadFileChunk()
 171          {
 172               if  ( this ._ThreadCallback  !=   null )
 173              {
 174                   this ._ThreadCallback( this ._RequestURL,  this ._FileName,  this ._Position,  this ._Length);
 175              }
 176          }
 177 
 178      }
 179 
 180       // 委托代理线程的所执行的方法签名一致
 181       public   delegate   void  ThreadCallbackHandler( string  S,  string  s,  int  I,  int  i);
 182 
 183       // 异常处理动作
 184       public   enum  ExceptionActions
 185      {
 186          Throw,
 187          CancelAll,
 188          Ignore,
 189          Retry
 190      }
 191 
 192       ///   <summary>
 193       ///  包含 Exception 事件数据的类
 194       ///   </summary>
 195       public   class  ExceptionEventArgs : System.EventArgs
 196      {
 197           private  System.Exception _Exception;
 198           private  ExceptionActions _ExceptionAction;
 199 
 200           private  DownLoadState _DownloadState;
 201 
 202           public  DownLoadState DownloadState
 203          {
 204               get
 205              {
 206                   return  _DownloadState;
 207              }
 208          }
 209 
 210           public  Exception Exception
 211          {
 212               get
 213              {
 214                   return  _Exception;
 215              }
 216          }
 217 
 218           public  ExceptionActions ExceptionAction
 219          {
 220               get
 221              {
 222                   return  _ExceptionAction;
 223              }
 224               set
 225              {
 226                  _ExceptionAction  =  value;
 227              }
 228          }
 229 
 230           internal  ExceptionEventArgs(System.Exception e, DownLoadState DownloadState)
 231          {
 232               this ._Exception  =  e;
 233               this ._DownloadState  =  DownloadState;
 234          }
 235      }
 236 
 237       ///   <summary>
 238       ///  包含 DownLoad 事件数据的类
 239       ///   </summary>
 240       public   class  DownLoadEventArgs : System.EventArgs
 241      {
 242           private  DownLoadState _DownloadState;
 243 
 244           public  DownLoadState DownloadState
 245          {
 246               get
 247              {
 248                   return  _DownloadState;
 249              }
 250          }
 251 
 252           public  DownLoadEventArgs(DownLoadState DownloadState)
 253          {
 254               this ._DownloadState  =  DownloadState;
 255          }
 256 
 257      }
 258 
 259       ///   <summary>
 260       ///  支持断点续传多线程下载的类
 261       ///   </summary>
 262       public   class  HttpWebClient
 263      {
 264           private   static   object  _SyncLockObject  =   new   object ();
 265 
 266           public   delegate   void  DataReceiveEventHandler(HttpWebClient Sender, DownLoadEventArgs e);
 267 
 268           public   event  DataReceiveEventHandler DataReceive;  // 接收字节数据事件
 269 
 270           public   delegate   void  ExceptionEventHandler(HttpWebClient Sender, ExceptionEventArgs e);
 271 
 272           public   event  ExceptionEventHandler ExceptionOccurrs;  // 发生异常事件
 273 
 274           private   int  _FileLength;  // 下载文件的总大小
 275 
 276           public   static  ArrayList threads;
 277 
 278           public   int  FileLength
 279          {
 280               get
 281              {
 282                   return  _FileLength;
 283              }
 284          }
 285 
 286           ///   <summary>
 287           ///  分块下载文件
 288           ///   </summary>
 289           ///   <param name="Address"> URL 地址 </param>
 290           ///   <param name="FileName"> 保存到本地的路径文件名 </param>
 291           ///   <param name="ChunksCount"> 块数,线程数 </param>
 292           public   void  DownloadFile( string  Address,  string  FileName,  int  ChunksCount)
 293          {
 294               int  p  =   0 //  position
 295               int  s  =   0 //  chunk size
 296               string  a  =   null ;
 297              HttpWebRequest hwrq;
 298              HttpWebResponse hwrp  =   null ;
 299               try
 300              {
 301 
 302                  hwrq  =  (HttpWebRequest)WebRequest.Create( this .GetUri(Address));
 303                   // hwrq.Timeout = 20000000;
 304                   // if (hwrq.HaveResponse == false)
 305                   //     return;
 306                   // hwrq.ProtocolVersion =HttpVersion.Version10;
 307                   // WebProxy wp = WebProxy.GetDefaultProxy();
 308                   // hwrq.Proxy = wp;
 309                  hwrq.Method  =   " GET " ;
 310                   try
 311                  {
 312                      hwrp  =  (HttpWebResponse)hwrq.GetResponse();
 313                  }
 314                   catch  (Exception e)
 315                  {
 316                       throw   new  Exception(e.Message);
 317                  }
 318 
 319                   long  L  =  hwrp.ContentLength;
 320 
 321                   // 如果文件太小, 就不用分多线程, 用一个线程下载即可. (目前控制在800K) 
 322                   // if (L < 800000)
 323                   // {
 324                   //     ChunksCount = 1;
 325                   // }
 326 
 327                  hwrq.Credentials  =   this .m_credentials;
 328 
 329                  L  =  ((L  ==   - 1
导言 创建一个数据访问层 创建一个业务逻辑层 母板页和站点导航 基本报表 使用ObjectDataSource展现数据 声明参数 编程设置ObjectDataSource的参数值 主/从 使用DropDownList过滤的主/从报表 使用两个DropDownList过滤的主/从报表 跨页面的主/从报表 使用GridView 和DetailView实现的主/从报表 自定义格式化 基于数据的自定义格式化 在GridView控件中使用TemplateField 在DetailsView控件中使用TemplateField 使用FormView 的模板 在GridView的页脚中显示统计信息 编辑插入和删除数据 概述插入、更新和删除数据 研究插入、更新和删除的关联事件 在ASP.NET页面中处理BLL/DAL层的异常 给编辑和新增界面增加验证控件 定制数据修改界面 实现开放式并发 为删除数据添加客户端确认 基于用户对修改数据进行限制 分页和排序 分页和排序报表数据 大数据量时提高分页的效率 排序自定义分页数据 创建自定义排序用户界面 自定义按钮行为 GridView里的Button 使用DataList和Repeater显示数据 用DataList和Repeater来显示数据 格式化DataList和Repeater的数据 使用DataList来一行显示多条记录 数据控件的嵌套 使用DataList和Repeater过滤数据 使用DropDownList过滤的主/从报表 跨页面的主/从报表 使用Repeater和DataList实现的主/从报表 使用DataList编辑和删除数据 综叙:在DataList里编辑和删除数据 批量更新 处理BLL和DAL的异常 在编辑和插入界面里添加验证控件 自定义DataList编辑界面 实现开放式并发 为删除数据添加客户端确认 基于用户对修改数据进行限制 DataList和Repeater的分页和排序 DataList和Repeater数据分页 DataList和Repeater数据排序(一) DataList和Repeater数据排序(二) DataList和Repeater数据排序(三) DataList和Repeater的自定义按钮行为 DataList和Repeater里的自定义button 从ASP.NET页面直接访问数据库 47 使用SqlDataSource 控件查询数据(Reeezak) 48 在SqlDataSource中使用参数化查询(Reeezak) 49 使用SqlDataSource插入、更新以及删除数据(Reeezak
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值