WebClientHelper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using SiwiFms.Helper;
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. using System.Windows.Forms;
  6. namespace Siwi.Helper
  7. {
  8. public class UpDownLoadFile
  9. {
  10. /// <summary>
  11. /// WebClient上传文件至服务器(不带进度条)
  12. /// </summary>
  13. /// <param name="fileNameFullPath">要上传的文件(全路径格式)</param>
  14. /// <param name="strUrlDirPath">Web服务器文件夹路径</param>
  15. /// <returns>True/False是否上传成功</returns>
  16. public bool UpLoadFile(string fileNameFullPath, string strUrlDirPath)
  17. {
  18. //得到要上传的文件文件名
  19. string fileName = fileNameFullPath.Substring(fileNameFullPath.LastIndexOf("//") + 1);
  20. //得到文件扩展名
  21. string fileExtension = Path.GetExtension(fileNameFullPath);
  22. //新文件名
  23. string NewFileName = GuidHelper.NewGuid + fileExtension;
  24. if (strUrlDirPath.EndsWith("/") == false)
  25. {
  26. strUrlDirPath = strUrlDirPath + "/";
  27. }
  28. //保存在服务器上时,将文件改名(示业务需要)
  29. strUrlDirPath = strUrlDirPath + NewFileName;
  30. // 创建WebClient实例
  31. var myWebClient = new WebClient();
  32. myWebClient.Credentials = CredentialCache.DefaultCredentials;
  33. // 将要上传的文件打开读进文件流
  34. var myFileStream = new FileStream(fileNameFullPath, FileMode.Open, FileAccess.Read);
  35. var myBinaryReader = new BinaryReader(myFileStream);
  36. try
  37. {
  38. byte[] postArray = myBinaryReader.ReadBytes((int)myFileStream.Length);
  39. //打开远程Web地址,将文件流写入
  40. Stream postStream = myWebClient.OpenWrite(strUrlDirPath, "PUT");
  41. if (postStream.CanWrite)
  42. {
  43. postStream.Write(postArray, 0, postArray.Length);
  44. }
  45. postStream.Close(); //关闭流
  46. return true;
  47. }
  48. catch
  49. {
  50. //MessageBox.Show("文件上传失败:" + exp.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  51. return false;
  52. }
  53. }
  54. /// <summary>
  55. /// 下载服务器文件至客户端(不带进度条)
  56. /// </summary>
  57. /// <param name="strUrlFilePath">要下载的Web服务器上的文件地址(全路径 如:http://www.abc.com/test.rar)</param>
  58. /// <param name="Dir">下载到的目录(存放位置,机地机器文件夹)</param>
  59. /// <returns>True/False是否上传成功</returns>
  60. public bool DownLoadFile(string strUrlFilePath, string strLocalDirPath)
  61. {
  62. // 创建WebClient实例
  63. var client = new WebClient();
  64. //被下载的文件名
  65. string fileName = strUrlFilePath.Substring(strUrlFilePath.LastIndexOf("/"));
  66. //另存为的绝对路径+文件名
  67. string Path = strLocalDirPath + fileName;
  68. try
  69. {
  70. WebRequest myWebRequest = WebRequest.Create(strUrlFilePath);
  71. }
  72. catch (Exception exp)
  73. {
  74. MessageBox.Show("文件下载失败:" + exp.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  75. }
  76. try
  77. {
  78. client.DownloadFile(strUrlFilePath, Path);
  79. return true;
  80. }
  81. catch (Exception exp)
  82. {
  83. MessageBox.Show("文件下载失败:" + exp.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  84. return false;
  85. }
  86. }
  87. /// <summary>
  88. /// 下载带进度条代码(普通进度条)
  89. /// </summary>
  90. /// <param name="URL">网址</param>
  91. /// <param name="Filename">文件名</param>
  92. /// <param name="Prog">普通进度条ProgressBar</param>
  93. /// <returns>True/False是否下载成功</returns>
  94. public bool DownLoadFile(string URL, string Filename, ProgressBar Prog)
  95. {
  96. try
  97. {
  98. var Myrq = (HttpWebRequest)WebRequest.Create(URL); //从URL地址得到一个WEB请求
  99. var myrp = (HttpWebResponse)Myrq.GetResponse(); //从WEB请求得到WEB响应
  100. long totalBytes = myrp.ContentLength; //从WEB响应得到总字节数
  101. Prog.Maximum = (int)totalBytes; //从总字节数得到进度条的最大值
  102. Stream st = myrp.GetResponseStream(); //从WEB请求创建流(读)
  103. Stream so = new FileStream(Filename, FileMode.Create); //创建文件流(写)
  104. long totalDownloadedByte = 0; //下载文件大小
  105. var by = new byte[1024];
  106. int osize = st.Read(by, 0, @by.Length); //读流
  107. while (osize > 0)
  108. {
  109. totalDownloadedByte = osize + totalDownloadedByte; //更新文件大小
  110. Application.DoEvents();
  111. so.Write(by, 0, osize); //写流
  112. Prog.Value = (int)totalDownloadedByte; //更新进度条
  113. osize = st.Read(by, 0, @by.Length); //读流
  114. }
  115. so.Close(); //关闭流
  116. st.Close(); //关闭流
  117. return true;
  118. }
  119. catch
  120. {
  121. return false;
  122. }
  123. }
  124. /// <summary>
  125. /// 下载带进度条代码(状态栏式进度条)
  126. /// </summary>
  127. /// <param name="URL">网址</param>
  128. /// <param name="Filename">文件名</param>
  129. /// <param name="Prog">状态栏式进度条ToolStripProgressBar</param>
  130. /// <returns>True/False是否下载成功</returns>
  131. public bool DownLoadFile(string URL, string Filename, ToolStripProgressBar Prog)
  132. {
  133. try
  134. {
  135. var Myrq = (HttpWebRequest)WebRequest.Create(URL); //从URL地址得到一个WEB请求
  136. var myrp = (HttpWebResponse)Myrq.GetResponse(); //从WEB请求得到WEB响应
  137. long totalBytes = myrp.ContentLength; //从WEB响应得到总字节数
  138. Prog.Maximum = (int)totalBytes; //从总字节数得到进度条的最大值
  139. Stream st = myrp.GetResponseStream(); //从WEB请求创建流(读)
  140. Stream so = new FileStream(Filename, FileMode.Create); //创建文件流(写)
  141. long totalDownloadedByte = 0; //下载文件大小
  142. var by = new byte[1024];
  143. int osize = st.Read(by, 0, @by.Length); //读流
  144. while (osize > 0)
  145. {
  146. totalDownloadedByte = osize + totalDownloadedByte; //更新文件大小
  147. Application.DoEvents();
  148. so.Write(by, 0, osize); //写流
  149. Prog.Value = (int)totalDownloadedByte; //更新进度条
  150. osize = st.Read(by, 0, @by.Length); //读流
  151. }
  152. so.Close(); //关闭流
  153. st.Close(); //关闭流
  154. return true;
  155. }
  156. catch
  157. {
  158. return false;
  159. }
  160. }
  161. }
  162. }