星火微课系统客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DownloadService.cs 6.7KB

pirms 4 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Common.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. namespace Common.system
  9. {
  10. /// <summary>
  11. /// 下载方法
  12. /// 创建人:赵耀
  13. /// 创建时间:2018年10月30日
  14. /// </summary>
  15. public class DownloadService
  16. {
  17. private string downloadUrl = "";//文件下载地址
  18. private string filePath = "";//文件保存路径
  19. private string method = "";//方法
  20. private long fromIndex = 0;//开始下载的位置
  21. private long toIndex = 0;//结束下载的位置
  22. private long count = 0;//总大小
  23. private long size = 524288;//每次下载大小 512kb
  24. private bool isRun = false;//是否正在进行
  25. public bool isFinish = false;//是否已下载完成{ get; private set; }
  26. public bool isStopped = true;//是否已停止{ get; private set; }
  27. /// <summary>
  28. /// 开始下载
  29. /// </summary>
  30. public event Action OnStart;
  31. /// <summary>
  32. /// 下载进度
  33. /// </summary>
  34. public event Action OnDownload;
  35. /// <summary>
  36. /// 完成下载
  37. /// </summary>
  38. public event Action OnFinsh;
  39. /// <summary>
  40. /// 断开网络连接
  41. /// </summary>
  42. public event Action OnDisconnect;
  43. public long GetDownloadedCount()
  44. {
  45. return this.count - this.toIndex + this.fromIndex - 1;
  46. }
  47. public void Stop()
  48. {
  49. this.isRun = false;
  50. }
  51. /// <summary>
  52. /// 开始下载
  53. /// </summary>
  54. /// <param name="info"></param>
  55. /// <param name="isReStart"></param>
  56. /// <returns></returns>
  57. public bool Start(TaskInfoModel info, bool isReStart)
  58. {
  59. this.downloadUrl = info.downloadUrl;
  60. this.fromIndex = info.fromIndex;
  61. this.toIndex = info.toIndex;
  62. this.method = info.method;
  63. this.filePath = info.filePath;
  64. this.count = info.count;
  65. this.isStopped = false;
  66. if (File.Exists(this.filePath))
  67. {
  68. if (isReStart)
  69. {
  70. File.Delete(this.filePath);
  71. File.Create(this.filePath).Close();
  72. }
  73. }
  74. else
  75. {
  76. File.Create(this.filePath).Close();
  77. }
  78. using (var file = File.Open(this.filePath, FileMode.Open))
  79. {
  80. this.fromIndex = info.fromIndex + file.Length;
  81. }
  82. if (this.fromIndex >= this.toIndex)
  83. {
  84. OnFineshHandler();
  85. this.isFinish = true;
  86. this.isStopped = true;
  87. return false;
  88. }
  89. OnStartHandler();
  90. this.isRun = true;
  91. new Action(() =>
  92. {
  93. WebResponse rsp;
  94. while (this.fromIndex < this.toIndex && isRun)
  95. {
  96. long to;
  97. if (this.fromIndex + this.size >= this.toIndex - 1)
  98. to = this.toIndex - 1;
  99. else
  100. to = this.fromIndex + size;
  101. try
  102. {
  103. using (rsp = HttpHelper.Download(this.downloadUrl, this.fromIndex, to, this.method))
  104. {
  105. Save(this.filePath, rsp.GetResponseStream());
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. string ErrMessage = "【下载】(DownloadService):" + ex.Message;
  111. LogHelper.WriteErrLog(ErrMessage, ex);
  112. OnDisconnectHandler();
  113. return;
  114. }
  115. }
  116. if (!this.isRun) this.isStopped = true;
  117. if (this.fromIndex >= this.toIndex)
  118. {
  119. this.isFinish = true;
  120. this.isStopped = true;
  121. OnFineshHandler();
  122. }
  123. }).BeginInvoke(null, null);
  124. return true;
  125. }
  126. /// <summary>
  127. /// 保存文件
  128. /// </summary>
  129. /// <param name="filePath"></param>
  130. /// <param name="stream"></param>
  131. private void Save(string filePath, Stream stream)
  132. {
  133. try
  134. {
  135. using (var writer = File.Open(filePath, FileMode.Append))
  136. {
  137. using (stream)
  138. {
  139. var repeatTimes = 0;
  140. byte[] buffer = new byte[1024];
  141. var length = 0;
  142. while ((length = stream.Read(buffer, 0, buffer.Length)) > 0 && this.isRun)
  143. {
  144. writer.Write(buffer, 0, length);
  145. this.fromIndex += length;
  146. if (repeatTimes % 5 == 0)
  147. {
  148. OnDownloadHandler();
  149. }
  150. repeatTimes++;
  151. }
  152. }
  153. }
  154. OnDownloadHandler();
  155. }
  156. catch (Exception)
  157. {
  158. //异常也不影响
  159. }
  160. }
  161. /// <summary>
  162. /// 开始下载
  163. /// </summary>
  164. private void OnStartHandler()
  165. {
  166. new Action(() =>
  167. {
  168. if (this.OnStart != null)
  169. this.OnStart.Invoke();
  170. }).BeginInvoke(null, null);
  171. }
  172. /// <summary>
  173. /// 完成下载
  174. /// </summary>
  175. private void OnFineshHandler()
  176. {
  177. new Action(() =>
  178. {
  179. if (this.OnFinsh != null)
  180. this.OnFinsh.Invoke();
  181. if (this.OnDownload != null)
  182. this.OnDownload.Invoke();
  183. }).BeginInvoke(null, null);
  184. }
  185. /// <summary>
  186. /// 下载进度
  187. /// </summary>
  188. private void OnDownloadHandler()
  189. {
  190. new Action(() =>
  191. {
  192. if (this.OnDownload != null)
  193. this.OnDownload.Invoke();
  194. }).BeginInvoke(null, null);
  195. }
  196. /// <summary>
  197. /// 断开网络连接
  198. /// </summary>
  199. private void OnDisconnectHandler()
  200. {
  201. new Action(() =>
  202. {
  203. if (this.OnDisconnect != null)
  204. this.OnDisconnect.Invoke();
  205. }).BeginInvoke(null, null);
  206. }
  207. }
  208. }