Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
2008-06-17 14:21
1241 查看
(一) . 概述
最近做了个C/S文件下载工具, 支持多任务, 多线程和断点续传功能. 其中部分代码是从网上找来的, 自己改了
许多Thread Bug, 并增加多任务, 断点续传等功能.
由于公司具有代码所有权, 不能将源代码共享. 自己对比较Asp.net感兴趣, 业余时间自己做了个简单的, 基于
Asp.net 2.0的, 目前能够执行对一个文件的下载任务, 但已经实现了多线程, 断点续传功能. 根据需要您可以增加
多任务 功能, 分享一下, 互相学习! 互相借鉴!
时间仓促, 此程序还没有做很多参数方面的优化. 可以作参考用.
(二).运行效果
";
25
26 DownLoadComponent.HttpWebClient x = new DownLoadComponent.HttpWebClient();
27
28 //注册 DataReceive 事件
29 x.DataReceive += new DownLoadComponent.HttpWebClient.DataReceiveEventHandler(this.x_DataReceive);
30 //注册 ExceptionOccurrs 事件
31 x.ExceptionOccurrs += new DownLoadComponent.HttpWebClient.ExceptionEventHandler(this.x_ExceptionOccurrs);
32
33 string Source = this.TextBox1.Text.Trim();
34 string FileName = Source.Substring(Source.LastIndexOf("/") + 1);
35 string Location= System.IO.Path.Combine( this.TextBox2.Text.Trim() , FileName);
36
37 //F: 源服务器文件; _f: 保存路径; 10: 自设定一个文件有几个线程下载.
38 x.DownloadFile(Source,Location , int.Parse(this.TextBox3.Text));
39
40 //Response.Write("正在下载文件");
41 this.btOK.Enabled = false;
42 this.btCancel.Enabled = true;
43 }
44
45 private void x_DataReceive(DownLoadComponent.HttpWebClient Sender, DownLoadComponent.DownLoadEventArgs e)
46 {
47
48 string f = e.DownloadState.FileName;
49 if (e.DownloadState.AttachmentName != null)
50 f = System.IO.Path.GetDirectoryName(f) + @"\" + e.DownloadState.AttachmentName;
51
52 using (System.IO.FileStream sw = new System.IO.FileStream(f, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite))
53 {
54 sw.Position = e.DownloadState.Position;
55 sw.Write(e.DownloadState.Data, 0, e.DownloadState.Data.Length);
56 sw.Close();
57 }
58 }
59
60 private void x_ExceptionOccurrs(DownLoadComponent.HttpWebClient Sender, DownLoadComponent.ExceptionEventArgs e)
61 {
62 System.Console.WriteLine(e.Exception.Message);
63 //发生异常重新下载相当于断点续传,你可以自己自行选择处理方式或自行处理
64 DownLoadComponent.HttpWebClient x = new DownLoadComponent.HttpWebClient();
65 x.DataReceive += new DownLoadComponent.HttpWebClient.DataReceiveEventHandler(this.x_DataReceive);
66 //订阅 ExceptionOccurrs 事件
67 //x.ExceptionOccurrs += new DownLoadComponent.HttpWebClient.ExceptionEventHandler(this.x_ExceptionOccurrs);
68
69 x.DownloadFileChunk(e.DownloadState.RequestURL, e.DownloadState.FileName, e.DownloadState.Position, e.DownloadState.Length);
70 e.ExceptionAction = DownLoadComponent.ExceptionActions.Ignore;
71 }
72 protected void btCancel_Click(object sender, EventArgs e)
73 {
74 if (DownLoadComponent.HttpWebClient.threads != null)
75 {
76 foreach (Thread t in DownLoadComponent.HttpWebClient.threads)
77 {
78 if (t.IsAlive)
79 {
80 t.Abort();
81 }
82 }
83
84 DownLoadComponent.HttpWebClient.threads.Clear();
85 }
86 System.Diagnostics.Process myproc = new System.Diagnostics.Process();
87 Process[] procs = (Process[])Process.GetProcessesByName("DW20.exe"); //得到所有打开的进程
88 try
89 {
90 foreach (Process proc in procs)
91 {
92 if (proc.CloseMainWindow() == false)
93 {
94 proc.Kill();
95 }
96 }
97 }
98 catch
99 { }
100 KillAllThreads();
101 this.btOK.Enabled = true;
102 this.btCancel.Enabled = false;
103 GC.Collect();
104
105 }
106
107 /// <summary>
108 /// 定期检查控制文件
109 /// </summary>
110 /// <param name="str"></param>
111 /// <returns>是否还继续监视(1: 正在下载中,继续监视; 0: 表示已经下载完毕,不用再检视)</returns>
112 [AjaxMethod()]// or [AjaxPro.AjaxMethod]
113 public bool CheckControlFiles(string strObjPath)
114 {
115 if (!WhetherDownloadFinished(strObjPath))
116 {
117 return true;
118 }
119 return false;
120 }
121
122 private bool WhetherDownloadFinished(string strObjPath)
123 {
124 DirectoryInfo df = new DirectoryInfo(strObjPath);
125 FileInfo[] fi = (FileInfo[])df.GetFiles("*.txt", SearchOption.TopDirectoryOnly);
126 HttpWebClient hwc = new HttpWebClient();
127 for (int i = 0; i < fi.Length; i++)
128 {
129 if (fi[i].FullName.Length > 12 && fi[i].FullName.Substring(fi[i].FullName.Length - 12) == "_Control.txt")
130 {
131 if (hwc.JudgeControlFileIfFinished(fi[i].FullName) == true)
132 {
133 hwc.DeleteControlFile(fi[i].FullName);
134 KillAllThreads();
135 return true;
136 }
137 }
138 }
139 return false;
140 }
141
142 private void KillAllThreads()
143 {
144 foreach (Thread t in HttpWebClient.threads)
145 {
146 if (t.IsAlive)
147 {
148 t.Abort();
149 }
150 }
151 HttpWebClient.threads.Clear();
152 }
153
154 }
155
(四).示例代码下载
http://files.cnblogs.com/MVP33650/MultiThreadDownLoadFile.rar
(五).Asp.net 2.0其它相关文章:
http://blog.csdn.net/ChengKing/category/288694.aspx
最近做了个C/S文件下载工具, 支持多任务, 多线程和断点续传功能. 其中部分代码是从网上找来的, 自己改了
许多Thread Bug, 并增加多任务, 断点续传等功能.
由于公司具有代码所有权, 不能将源代码共享. 自己对比较Asp.net感兴趣, 业余时间自己做了个简单的, 基于
Asp.net 2.0的, 目前能够执行对一个文件的下载任务, 但已经实现了多线程, 断点续传功能. 根据需要您可以增加
多任务 功能, 分享一下, 互相学习! 互相借鉴!
时间仓促, 此程序还没有做很多参数方面的优化. 可以作参考用.
(二).运行效果
";
25
26 DownLoadComponent.HttpWebClient x = new DownLoadComponent.HttpWebClient();
27
28 //注册 DataReceive 事件
29 x.DataReceive += new DownLoadComponent.HttpWebClient.DataReceiveEventHandler(this.x_DataReceive);
30 //注册 ExceptionOccurrs 事件
31 x.ExceptionOccurrs += new DownLoadComponent.HttpWebClient.ExceptionEventHandler(this.x_ExceptionOccurrs);
32
33 string Source = this.TextBox1.Text.Trim();
34 string FileName = Source.Substring(Source.LastIndexOf("/") + 1);
35 string Location= System.IO.Path.Combine( this.TextBox2.Text.Trim() , FileName);
36
37 //F: 源服务器文件; _f: 保存路径; 10: 自设定一个文件有几个线程下载.
38 x.DownloadFile(Source,Location , int.Parse(this.TextBox3.Text));
39
40 //Response.Write("正在下载文件");
41 this.btOK.Enabled = false;
42 this.btCancel.Enabled = true;
43 }
44
45 private void x_DataReceive(DownLoadComponent.HttpWebClient Sender, DownLoadComponent.DownLoadEventArgs e)
46 {
47
48 string f = e.DownloadState.FileName;
49 if (e.DownloadState.AttachmentName != null)
50 f = System.IO.Path.GetDirectoryName(f) + @"\" + e.DownloadState.AttachmentName;
51
52 using (System.IO.FileStream sw = new System.IO.FileStream(f, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite))
53 {
54 sw.Position = e.DownloadState.Position;
55 sw.Write(e.DownloadState.Data, 0, e.DownloadState.Data.Length);
56 sw.Close();
57 }
58 }
59
60 private void x_ExceptionOccurrs(DownLoadComponent.HttpWebClient Sender, DownLoadComponent.ExceptionEventArgs e)
61 {
62 System.Console.WriteLine(e.Exception.Message);
63 //发生异常重新下载相当于断点续传,你可以自己自行选择处理方式或自行处理
64 DownLoadComponent.HttpWebClient x = new DownLoadComponent.HttpWebClient();
65 x.DataReceive += new DownLoadComponent.HttpWebClient.DataReceiveEventHandler(this.x_DataReceive);
66 //订阅 ExceptionOccurrs 事件
67 //x.ExceptionOccurrs += new DownLoadComponent.HttpWebClient.ExceptionEventHandler(this.x_ExceptionOccurrs);
68
69 x.DownloadFileChunk(e.DownloadState.RequestURL, e.DownloadState.FileName, e.DownloadState.Position, e.DownloadState.Length);
70 e.ExceptionAction = DownLoadComponent.ExceptionActions.Ignore;
71 }
72 protected void btCancel_Click(object sender, EventArgs e)
73 {
74 if (DownLoadComponent.HttpWebClient.threads != null)
75 {
76 foreach (Thread t in DownLoadComponent.HttpWebClient.threads)
77 {
78 if (t.IsAlive)
79 {
80 t.Abort();
81 }
82 }
83
84 DownLoadComponent.HttpWebClient.threads.Clear();
85 }
86 System.Diagnostics.Process myproc = new System.Diagnostics.Process();
87 Process[] procs = (Process[])Process.GetProcessesByName("DW20.exe"); //得到所有打开的进程
88 try
89 {
90 foreach (Process proc in procs)
91 {
92 if (proc.CloseMainWindow() == false)
93 {
94 proc.Kill();
95 }
96 }
97 }
98 catch
99 { }
100 KillAllThreads();
101 this.btOK.Enabled = true;
102 this.btCancel.Enabled = false;
103 GC.Collect();
104
105 }
106
107 /// <summary>
108 /// 定期检查控制文件
109 /// </summary>
110 /// <param name="str"></param>
111 /// <returns>是否还继续监视(1: 正在下载中,继续监视; 0: 表示已经下载完毕,不用再检视)</returns>
112 [AjaxMethod()]// or [AjaxPro.AjaxMethod]
113 public bool CheckControlFiles(string strObjPath)
114 {
115 if (!WhetherDownloadFinished(strObjPath))
116 {
117 return true;
118 }
119 return false;
120 }
121
122 private bool WhetherDownloadFinished(string strObjPath)
123 {
124 DirectoryInfo df = new DirectoryInfo(strObjPath);
125 FileInfo[] fi = (FileInfo[])df.GetFiles("*.txt", SearchOption.TopDirectoryOnly);
126 HttpWebClient hwc = new HttpWebClient();
127 for (int i = 0; i < fi.Length; i++)
128 {
129 if (fi[i].FullName.Length > 12 && fi[i].FullName.Substring(fi[i].FullName.Length - 12) == "_Control.txt")
130 {
131 if (hwc.JudgeControlFileIfFinished(fi[i].FullName) == true)
132 {
133 hwc.DeleteControlFile(fi[i].FullName);
134 KillAllThreads();
135 return true;
136 }
137 }
138 }
139 return false;
140 }
141
142 private void KillAllThreads()
143 {
144 foreach (Thread t in HttpWebClient.threads)
145 {
146 if (t.IsAlive)
147 {
148 t.Abort();
149 }
150 }
151 HttpWebClient.threads.Clear();
152 }
153
154 }
155
(四).示例代码下载
http://files.cnblogs.com/MVP33650/MultiThreadDownLoadFile.rar
(五).Asp.net 2.0其它相关文章:
http://blog.csdn.net/ChengKing/category/288694.aspx
相关文章推荐
- Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- [导入]Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- 【转】Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- php支持分块与断点续传文件下载功能代码
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载续][重点推荐控件]
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- php支持分块与断点续传文件下载功能代码
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net 2.0 装载XML文件中数据到Excel文件中(示例代码下载)
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载][续]
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)