1 /// <summary>
2 /// Author: [ ChengKing(ZhengJian) ]
3 /// Blog: Http://blog.csdn.net/ChengKing
4 /// 注:从网上找了个优秀代码
5 /// 扩展如下功能:
6 /// 1. 解决一些线程相关的Bug;
7 /// 2.扩展用控制文件实现断点续传功能.
8 /// </summary>
9 public partial class _Default : System.Web.UI.Page
10 {
11 //全局变量
12 private static object _SyncLockObject = new object();
13
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 Utility.RegisterTypeForAjax(typeof(_Default));
17 //this.TextBox1.Text =
"http://download.csdn.net/filedown
/aHR0cDovL2Rvd25sb2FkMS5jc2RuLm5ldC9kb3duMy8yMDA3MDUwNy8wNzE4MDIwNzY4OC5yYXI=!177258";
18
19
20 this.TextBox1.Text = "http://www.cnblogs.com/Files/ChengKing/智能象棋游戏(T1).rar";
21 }
22 protected void btOK_Click(object sender, EventArgs e)
23 {
24 this.Label1.Text = "状态: 正在下载";
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