DownloadTools.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. namespace Common
  5. {
  6. public class DownloadTools
  7. {
  8. public string DownloadFile(string SavePath, string DownloadUrl, string FileName)
  9. {
  10. if (!Directory.Exists(SavePath))
  11. {
  12. Directory.CreateDirectory(SavePath);
  13. }
  14. string path = SavePath + "\\" + FileName;
  15. string requestUriString = DownloadUrl + "/" + FileName;
  16. try
  17. {
  18. WebResponse response = WebRequest.Create(requestUriString).GetResponse();
  19. if (response.ContentLength > 0)
  20. {
  21. try
  22. {
  23. long contentLength = response.ContentLength;
  24. try
  25. {
  26. Stream responseStream = response.GetResponseStream();
  27. StreamReader reader = new StreamReader(responseStream);
  28. byte[] buffer = new byte[contentLength];
  29. int count = buffer.Length;
  30. int offset = 0;
  31. while (contentLength > 0)
  32. {
  33. int num4 = responseStream.Read(buffer, offset, count);
  34. if (num4 == 0)
  35. {
  36. break;
  37. }
  38. offset += num4;
  39. count -= num4;
  40. float num5 = ((float)offset) / 1024f;
  41. float num6 = ((float)buffer.Length) / 1024f;
  42. Convert.ToInt32((float)((num5 / num6) * 100f));
  43. }
  44. FileStream stream2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
  45. stream2.Write(buffer, 0, buffer.Length);
  46. responseStream.Close();
  47. reader.Close();
  48. stream2.Close();
  49. return FileName;
  50. }
  51. catch
  52. {
  53. return null;
  54. }
  55. }
  56. catch
  57. {
  58. return null;
  59. }
  60. }
  61. }
  62. catch
  63. {
  64. return null;
  65. }
  66. return null;
  67. }
  68. public bool IsDownloadFile(string SavePath, string DownloadUrl, string FileName)
  69. {
  70. if (!Directory.Exists(SavePath))
  71. {
  72. Directory.CreateDirectory(SavePath);
  73. }
  74. string path = SavePath + "\\" + FileName;
  75. string requestUriString = DownloadUrl + "/" + FileName;
  76. try
  77. {
  78. WebResponse response = WebRequest.Create(requestUriString).GetResponse();
  79. if (response.ContentLength > 0)
  80. {
  81. try
  82. {
  83. long contentLength = response.ContentLength;
  84. try
  85. {
  86. Stream responseStream = response.GetResponseStream();
  87. StreamReader reader = new StreamReader(responseStream);
  88. byte[] buffer = new byte[contentLength];
  89. int count = buffer.Length;
  90. int offset = 0;
  91. while (contentLength > 0)
  92. {
  93. int num4 = responseStream.Read(buffer, offset, count);
  94. if (num4 == 0)
  95. {
  96. break;
  97. }
  98. offset += num4;
  99. count -= num4;
  100. float num5 = ((float)offset) / 1024f;
  101. float num6 = ((float)buffer.Length) / 1024f;
  102. Convert.ToInt32((float)((num5 / num6) * 100f));
  103. }
  104. FileStream stream2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
  105. stream2.SetLength(0);
  106. stream2.Write(buffer, 0, buffer.Length);
  107. responseStream.Close();
  108. reader.Close();
  109. stream2.Close();
  110. return true;
  111. }
  112. catch (Exception e)
  113. {
  114. throw new Exception( e.Message);
  115. }
  116. }
  117. catch (Exception e)
  118. {
  119. throw new Exception(e.Message);
  120. }
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. throw new Exception(e.Message);
  126. }
  127. return false;
  128. }
  129. }
  130. }