星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DownloadService.cs 6.6KB

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