ProgressBar.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using Newtonsoft.Json.Linq;
  3. using System.Windows.Forms;
  4. using System.Threading;
  5. using FtpConnection;
  6. using Ini;
  7. using FileInfoClass;
  8. namespace ftpUploadDLL.Weblogic
  9. {
  10. public partial class ProgressBar : Form
  11. {
  12. FileClass fileclass = new FileClass();
  13. IniFile ini = new IniFile();
  14. private string logPath = "";
  15. int fileCount = 100;
  16. string Params = "";
  17. Thread th;
  18. public string currentPath = "";
  19. public string serverPath = "";
  20. public string serverFTPName = "";
  21. public string serverFTPPassword = "pryk@";
  22. public string ftpIP = "";
  23. public string ftpDefaultFolder = "browser";
  24. public static JArray fileStringArray = new JArray();
  25. public string outstring = "";
  26. private volatile bool canStop = false;
  27. private volatile bool Stopend = true;
  28. public ProgressBar(string inParams)
  29. {
  30. InitializeComponent();
  31. Params = inParams;
  32. //ProgressTest(inParams);
  33. if (th == null || !th.IsAlive)
  34. {
  35. th = new Thread(Run);
  36. th.IsBackground = true;
  37. th.Start();
  38. }
  39. }
  40. private void Run()
  41. {
  42. string errorCode = "0";
  43. string errorMessage = "";
  44. JObject jObject = new JObject();
  45. //获取指定目录下所有文件数量(实际过程忽略)
  46. //获取文件信息并显示进度
  47. //设置进度条基础属性
  48. try
  49. {
  50. Thread.Sleep(1000);
  51. this.button1.Enabled = false;
  52. this.myProgressBar1.Value = 0;
  53. this.myProgressBar1.Minimum = 0;
  54. this.TopMost = false;
  55. this.BringToFront();
  56. this.TopMost = true;
  57. jObject.Add("errorCode", "-1");
  58. jObject.Add("errorMessage", "上传进行中断或取消");
  59. outstring = jObject.ToString();
  60. outstring = UploadData(Params);
  61. }
  62. catch (Exception ex2)
  63. {
  64. errorCode = "-1";
  65. errorMessage = ex2.Message;
  66. JObject jObject2 = new JObject();
  67. jObject2.Add("errorCode", errorCode);
  68. jObject2.Add("errorMessage", errorMessage);
  69. outstring = jObject2.ToString();
  70. }
  71. }
  72. public string UploadData(string inParams)
  73. {
  74. //System.Diagnostics.Debugger.Launch();
  75. fileStringArray = new JArray();
  76. FtpOperationClass ftpObj = new FtpOperationClass();
  77. logPath=ini.IniReadValue("LOG", "path");
  78. string errorCode = "0";
  79. string errorMessage = "";
  80. JObject jObject = new JObject();
  81. try
  82. {
  83. JObject inputEmpParamsObj = JObject.Parse(inParams);
  84. var paramsdata = inputEmpParamsObj["params"][0];
  85. string filePath = paramsdata["filePath"].ToString(); //文档路径
  86. fileStringArray = ftpObj.Checkpath(filePath,out errorMessage); //1.核验文件夹或文件是否正常,生成上传路径
  87. if (errorMessage == "")
  88. {
  89. if (fileStringArray.Count > 0)
  90. {
  91. this.myProgressBar1.Maximum = fileStringArray.Count;
  92. var hisResultobj = inputEmpParamsObj["hisResult"];
  93. errorCode = hisResultobj["errorCode"].ToString();
  94. errorMessage = hisResultobj["errorMessage"].ToString();
  95. var resultObj = hisResultobj["result"];
  96. if (resultObj != null)
  97. {
  98. ftpIP = resultObj[0]["ftpIP"].ToString(); //ftp路径
  99. serverPath = resultObj[0]["ftpToPath"].ToString(); //ftp路径
  100. serverFTPName = resultObj[0]["serverFTPName"].ToString(); //ftp用户
  101. serverFTPPassword = resultObj[0]["serverFTPPassword"].ToString(); //ftp密码
  102. if (errorCode == "0")
  103. {
  104. if (fileStringArray.Count > 0)
  105. {
  106. ///上传文件
  107. int listnum = 0;
  108. foreach (JObject fileObj in fileStringArray)
  109. {
  110. if (canStop) {
  111. jObject.Add("errorCode", "-1");
  112. jObject.Add("errorMessage", "中断");
  113. Stopend = false;
  114. return jObject.ToString();
  115. }
  116. listnum = listnum + 1;
  117. string ftpurl = serverPath;
  118. string fileurl = fileObj["value"]!.ToString();
  119. string pathName = fileObj["pathName"]!.ToString();
  120. if ((pathName == "") || (pathName == null) || (pathName == string.Empty))
  121. {
  122. }
  123. else {
  124. ftpurl = serverPath + pathName + "/";
  125. }
  126. this.Text = "文件上传中:" + fileurl;
  127. this.textBox1.AppendText("当前进度:" + fileurl + "\r\n");
  128. this.textBox1.AppendText(" 到 FTP: " + ftpurl + "----->");
  129. fileclass.WriteLogFile(logPath, "当前进度:" + fileurl + "\r\n"+ " 到 FTP: " + ftpurl + "----->");
  130. if (this.IsHandleCreated)
  131. {
  132. this.BeginInvoke(new Action(() =>
  133. {
  134. this.myProgressBar1.Value = listnum;
  135. }));
  136. }
  137. string rtn = ftpObj.Upload(fileurl, ftpurl, ftpIP, serverFTPName, serverFTPPassword);
  138. JObject rtntobj = JObject.Parse(rtn);
  139. errorCode = rtntobj["errorCode"].ToString();
  140. errorMessage = rtntobj["errorMessage"].ToString();
  141. if (errorCode != "0")
  142. {
  143. this.textBox1.AppendText("失败:" + rtntobj.ToString() + "\r\n");
  144. fileclass.WriteLogFile(logPath, "失败:" + rtntobj.ToString() + "\r\n");
  145. return rtntobj.ToString();
  146. }
  147. else
  148. {
  149. this.textBox1.AppendText("成功" + "\r\n");
  150. fileclass.WriteLogFile(logPath, "成功" + "\r\n");
  151. }
  152. }
  153. }
  154. else
  155. {
  156. errorMessage = "已是最新版,无需更新!";
  157. }
  158. if (errorCode == "0")
  159. {
  160. this.button1.Enabled = true;
  161. }
  162. }
  163. }
  164. }
  165. else
  166. {
  167. errorCode = "-1";
  168. errorMessage = "文档路径校验失败,请核实!!!";
  169. }
  170. }
  171. else {
  172. errorCode = "-1";
  173. }
  174. }
  175. catch (Exception ex2)
  176. {
  177. errorCode = "-1";
  178. errorMessage = ex2.Message;
  179. }
  180. jObject.Add("errorCode", errorCode);
  181. jObject.Add("errorMessage", errorMessage);
  182. return jObject.ToString();
  183. }
  184. private void myProgressBar1_Click(object sender, EventArgs e)
  185. {
  186. }
  187. private void textBox1_TextChanged(object sender, EventArgs e)
  188. {
  189. }
  190. private void button1_Click(object sender, EventArgs e)
  191. {
  192. if (th == null || !th.IsAlive)
  193. {
  194. this.Close();
  195. th.Abort();
  196. }
  197. }
  198. private void cancelbutton_Click(object sender, EventArgs e)
  199. {
  200. canStop = true;
  201. while (!Stopend)
  202. {
  203. int Sleepnum = 1000;
  204. Thread.Sleep(Sleepnum);
  205. }
  206. if (th == null || !th.IsAlive)
  207. {
  208. this.Close();
  209. th.Abort();
  210. }
  211. }
  212. }
  213. }