| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using System;
- using Newtonsoft.Json.Linq;
- using System.Windows.Forms;
- using System.Threading;
- using FtpConnection;
- using Ini;
- using FileInfoClass;
- namespace ftpUploadDLL.Weblogic
- {
- public partial class ProgressBar : Form
- {
- FileClass fileclass = new FileClass();
- IniFile ini = new IniFile();
- private string logPath = "";
- int fileCount = 100;
- string Params = "";
- Thread th;
- public string currentPath = "";
- public string serverPath = "";
- public string serverFTPName = "";
- public string serverFTPPassword = "pryk@";
- public string ftpIP = "";
- public string ftpDefaultFolder = "browser";
- public static JArray fileStringArray = new JArray();
- public string outstring = "";
- private volatile bool canStop = false;
- private volatile bool Stopend = true;
- public ProgressBar(string inParams)
- {
- InitializeComponent();
- Params = inParams;
- //ProgressTest(inParams);
- if (th == null || !th.IsAlive)
- {
- th = new Thread(Run);
- th.IsBackground = true;
- th.Start();
- }
- }
- private void Run()
- {
- string errorCode = "0";
- string errorMessage = "";
- JObject jObject = new JObject();
- //获取指定目录下所有文件数量(实际过程忽略)
- //获取文件信息并显示进度
- //设置进度条基础属性
- try
- {
- Thread.Sleep(1000);
- this.button1.Enabled = false;
- this.myProgressBar1.Value = 0;
- this.myProgressBar1.Minimum = 0;
- this.TopMost = false;
- this.BringToFront();
- this.TopMost = true;
- jObject.Add("errorCode", "-1");
- jObject.Add("errorMessage", "上传进行中断或取消");
- outstring = jObject.ToString();
- outstring = UploadData(Params);
-
- }
- catch (Exception ex2)
- {
- errorCode = "-1";
- errorMessage = ex2.Message;
- JObject jObject2 = new JObject();
- jObject2.Add("errorCode", errorCode);
- jObject2.Add("errorMessage", errorMessage);
- outstring = jObject2.ToString();
- }
-
- }
- public string UploadData(string inParams)
- {
- //System.Diagnostics.Debugger.Launch();
- fileStringArray = new JArray();
- FtpOperationClass ftpObj = new FtpOperationClass();
- logPath=ini.IniReadValue("LOG", "path");
- string errorCode = "0";
- string errorMessage = "";
- JObject jObject = new JObject();
- try
- {
- JObject inputEmpParamsObj = JObject.Parse(inParams);
- var paramsdata = inputEmpParamsObj["params"][0];
- string filePath = paramsdata["filePath"].ToString(); //文档路径
- fileStringArray = ftpObj.Checkpath(filePath,out errorMessage); //1.核验文件夹或文件是否正常,生成上传路径
- if (errorMessage == "")
- {
- if (fileStringArray.Count > 0)
- {
- this.myProgressBar1.Maximum = fileStringArray.Count;
- var hisResultobj = inputEmpParamsObj["hisResult"];
- errorCode = hisResultobj["errorCode"].ToString();
- errorMessage = hisResultobj["errorMessage"].ToString();
- var resultObj = hisResultobj["result"];
- if (resultObj != null)
- {
- ftpIP = resultObj[0]["ftpIP"].ToString(); //ftp路径
- serverPath = resultObj[0]["ftpToPath"].ToString(); //ftp路径
- serverFTPName = resultObj[0]["serverFTPName"].ToString(); //ftp用户
- serverFTPPassword = resultObj[0]["serverFTPPassword"].ToString(); //ftp密码
- if (errorCode == "0")
- {
- if (fileStringArray.Count > 0)
- {
- ///上传文件
- int listnum = 0;
- foreach (JObject fileObj in fileStringArray)
- {
- if (canStop) {
- jObject.Add("errorCode", "-1");
- jObject.Add("errorMessage", "中断");
- Stopend = false;
- return jObject.ToString();
- }
- listnum = listnum + 1;
- string ftpurl = serverPath;
- string fileurl = fileObj["value"]!.ToString();
- string pathName = fileObj["pathName"]!.ToString();
- if ((pathName == "") || (pathName == null) || (pathName == string.Empty))
- {
- }
- else {
- ftpurl = serverPath + pathName + "/";
- }
- this.Text = "文件上传中:" + fileurl;
- this.textBox1.AppendText("当前进度:" + fileurl + "\r\n");
- this.textBox1.AppendText(" 到 FTP: " + ftpurl + "----->");
- fileclass.WriteLogFile(logPath, "当前进度:" + fileurl + "\r\n"+ " 到 FTP: " + ftpurl + "----->");
- if (this.IsHandleCreated)
- {
- this.BeginInvoke(new Action(() =>
- {
- this.myProgressBar1.Value = listnum;
- }));
- }
- string rtn = ftpObj.Upload(fileurl, ftpurl, ftpIP, serverFTPName, serverFTPPassword);
- JObject rtntobj = JObject.Parse(rtn);
- errorCode = rtntobj["errorCode"].ToString();
- errorMessage = rtntobj["errorMessage"].ToString();
- if (errorCode != "0")
- {
- this.textBox1.AppendText("失败:" + rtntobj.ToString() + "\r\n");
- fileclass.WriteLogFile(logPath, "失败:" + rtntobj.ToString() + "\r\n");
- return rtntobj.ToString();
- }
- else
- {
- this.textBox1.AppendText("成功" + "\r\n");
- fileclass.WriteLogFile(logPath, "成功" + "\r\n");
- }
- }
- }
- else
- {
- errorMessage = "已是最新版,无需更新!";
- }
- if (errorCode == "0")
- {
- this.button1.Enabled = true;
- }
- }
- }
- }
- else
- {
- errorCode = "-1";
- errorMessage = "文档路径校验失败,请核实!!!";
- }
- }
- else {
- errorCode = "-1";
- }
- }
- catch (Exception ex2)
- {
- errorCode = "-1";
- errorMessage = ex2.Message;
- }
- jObject.Add("errorCode", errorCode);
- jObject.Add("errorMessage", errorMessage);
- return jObject.ToString();
- }
- private void myProgressBar1_Click(object sender, EventArgs e)
- {
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (th == null || !th.IsAlive)
- {
- this.Close();
- th.Abort();
- }
-
- }
- private void cancelbutton_Click(object sender, EventArgs e)
- {
- canStop = true;
- while (!Stopend)
- {
- int Sleepnum = 1000;
- Thread.Sleep(Sleepnum);
- }
- if (th == null || !th.IsAlive)
- {
- this.Close();
- th.Abort();
- }
- }
- }
- }
|