星火微课系统客户端
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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 count - toIndex + fromIndex - 1;
  43. }
  44. public void Stop()
  45. {
  46. 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. downloadUrl = info.downloadUrl;
  57. fromIndex = info.fromIndex;
  58. toIndex = info.toIndex;
  59. method = info.method;
  60. filePath = info.filePath;
  61. count = info.count;
  62. isStopped = false;
  63. if (File.Exists(filePath))
  64. {
  65. if (isReStart)
  66. {
  67. File.Delete(filePath);
  68. File.Create(filePath).Close();
  69. }
  70. }
  71. else
  72. {
  73. File.Create(filePath).Close();
  74. }
  75. using (FileStream file = File.Open(filePath, FileMode.Open))
  76. {
  77. fromIndex = info.fromIndex + file.Length;
  78. }
  79. if (fromIndex >= toIndex)
  80. {
  81. OnFineshHandler();
  82. isFinish = true;
  83. isStopped = true;
  84. return false;
  85. }
  86. OnStartHandler();
  87. isRun = true;
  88. new Action(() =>
  89. {
  90. WebResponse rsp;
  91. while (fromIndex < toIndex && isRun)
  92. {
  93. long to;
  94. if (fromIndex + size >= toIndex - 1)
  95. {
  96. to = toIndex - 1;
  97. }
  98. else
  99. {
  100. to = fromIndex + size;
  101. }
  102. try
  103. {
  104. using (rsp = ZHttpUtil.Download
  105. (
  106. downloadUrl,
  107. fromIndex,
  108. to,
  109. method
  110. ))
  111. {
  112. Save(filePath, rsp.GetResponseStream());
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. string ErrMessage = "【下载】(DownloadService):" + ex.Message;
  118. LogHelper.WriteErrLog(ErrMessage, ex);
  119. OnDisconnectHandler();
  120. return;
  121. }
  122. }
  123. if (!isRun)
  124. {
  125. isStopped = true;
  126. }
  127. if (fromIndex >= toIndex)
  128. {
  129. isFinish = true;
  130. isStopped = true;
  131. OnFineshHandler();
  132. }
  133. }).BeginInvoke(null, null);
  134. return true;
  135. }
  136. /// <summary>
  137. /// 保存文件
  138. /// </summary>
  139. /// <param name="filePath"></param>
  140. /// <param name="stream"></param>
  141. private void Save(string filePath, Stream stream)
  142. {
  143. try
  144. {
  145. using (FileStream writer = File.Open(filePath, FileMode.Append))
  146. {
  147. using (stream)
  148. {
  149. int repeatTimes = 0;
  150. byte[] buffer = new byte[1024];
  151. int length = 0;
  152. while ((length = stream.Read(buffer, 0, buffer.Length)) > 0 && isRun)
  153. {
  154. writer.Write(buffer, 0, length);
  155. fromIndex += length;
  156. if (repeatTimes % 5 == 0)
  157. {
  158. OnDownloadHandler();
  159. }
  160. repeatTimes++;
  161. }
  162. }
  163. }
  164. OnDownloadHandler();
  165. }
  166. catch (Exception)
  167. {
  168. //异常也不影响
  169. }
  170. }
  171. /// <summary>
  172. /// 开始下载
  173. /// </summary>
  174. private void OnStartHandler()
  175. {
  176. new Action(() =>
  177. {
  178. if (OnStart != null)
  179. {
  180. OnStart.Invoke();
  181. }
  182. }).BeginInvoke(null, null);
  183. }
  184. /// <summary>
  185. /// 完成下载
  186. /// </summary>
  187. private void OnFineshHandler()
  188. {
  189. new Action(() =>
  190. {
  191. if (OnFinsh != null)
  192. {
  193. OnFinsh.Invoke();
  194. }
  195. if (OnDownload != null)
  196. {
  197. OnDownload.Invoke();
  198. }
  199. }).BeginInvoke(null, null);
  200. }
  201. /// <summary>
  202. /// 下载进度
  203. /// </summary>
  204. private void OnDownloadHandler()
  205. {
  206. new Action(() =>
  207. {
  208. if (OnDownload != null)
  209. {
  210. OnDownload.Invoke();
  211. }
  212. }).BeginInvoke(null, null);
  213. }
  214. /// <summary>
  215. /// 断开网络连接
  216. /// </summary>
  217. private void OnDisconnectHandler()
  218. {
  219. new Action(() =>
  220. {
  221. if (OnDisconnect != null)
  222. {
  223. OnDisconnect.Invoke();
  224. }
  225. }).BeginInvoke(null, null);
  226. }
  227. }
  228. }