Ejemplo n.º 1
0
 public static void AddToDoUploadToControl(ListView listView)
 {
     listView.Items.Clear();
     foreach (Dictionary <string, string> item in ToDoUploadList)
     {
         ListViewItem viewItem = new ListViewItem(item["num"]);
         viewItem.SubItems.Add(item["filename"]);
         viewItem.SubItems.Add(item["filesize"]);
         viewItem.SubItems.Add(item["process"]);
         viewItem.SubItems.Add(item["status"]);
         FileInfoEntity entity = new FileInfoEntity();
         entity.LocalURL       = item["localurl"];
         entity.AbsolutionPath = item["absolutionPath"];
         entity.FileName       = item["filename"];
         entity.FileSize       = item["filesize"];
         //viewItem.Tag = item["type"];
         viewItem.Tag = entity;
         listView.Items.Add(viewItem);
     }
 }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                button1.Enabled = false;
                ////获取选中的服务器列表key
                string serverKey = cb_remoteServer.Text;
                //string privateKey = GetPrivateKey();
                //if (privateKey == null || privateKey.Equals(string.Empty))
                //{
                //    MessageBox.Show("请求操作秘钥失败, 请稍后重试!", "提示");
                //    return;
                //}
                ListView.ListViewItemCollection collection = listView2.Items;
                for (int i = 0; i < collection.Count; i++)
                {
                    Thread.Sleep(120);
                    FileInfoEntity entity = (FileInfoEntity)collection[i].Tag;
                    //1.1发送文件名字,文件路径和文件字节大小 方便后端做好接受准备
                    string responseMessage = SendFileInfo(entity.LocalURL, entity.FileName, entity.FileSize, serverKey);
                    Console.WriteLine("上传文件信息成功:" + responseMessage);
                    //修改客户端界面数据效果
                    collection[i].Selected              = true;
                    collection[i].BackColor             = Color.Red;
                    collection[i].SubItems[4].ForeColor = Color.Blue;
                    collection[i].SubItems[4].Text      = "上传中";
                    collection[i].SubItems[3].Text      = "50%";
                    Thread.Sleep(120);
                    if ("success".Equals(responseMessage))
                    {
                        try
                        {
                            //1.2发送文件字节数组
                            using (FileStream fs = new FileStream(entity.AbsolutionPath, FileMode.Open))
                            {
                                byte[] data = new byte[fs.Length];
                                //int count = fs.Read(data, 0, data.Length);

                                //s
                                Console.WriteLine("流长度:" + fs.Length);
                                int idx      = 0;
                                int totalLen = data.Length;
                                int readLen  = 0;
                                while (idx < totalLen)
                                {
                                    readLen = fs.Read(data, idx, totalLen - idx);
                                    if (readLen > 0)
                                    {
                                        idx = idx + readLen;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                Console.WriteLine("实际长度:" + idx);

                                //e

                                Console.WriteLine("准备上传文件..");
                                string fileUploadResponse = SocketUtil.UploadFile(ClientCache.GetCurrentServerIP(), Convert.ToInt32(ClientCache.GetInterflowPort()), data);
                                Console.WriteLine("上传完成:" + fileUploadResponse);
                                JObject json    = JObject.Parse(fileUploadResponse);
                                string  message = json["message"].ToString();
                                if ("success".Equals(message))
                                {
                                    collection[i].Selected              = false;
                                    collection[i].BackColor             = Color.Green;
                                    collection[i].SubItems[4].ForeColor = Color.Blue;
                                    collection[i].SubItems[4].Text      = "上传完成";
                                    collection[i].SubItems[3].Text      = "100%";
                                }
                                else
                                {
                                    collection[i].Selected              = false;
                                    collection[i].BackColor             = Color.Red;
                                    collection[i].SubItems[4].ForeColor = Color.Blue;
                                    collection[i].SubItems[4].Text      = "上传失败";
                                    collection[i].SubItems[3].Text      = "100%";
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("当前文件出现错误,跳过" + ex.Message);
                            collection[i].Selected              = false;
                            collection[i].BackColor             = Color.Green;
                            collection[i].SubItems[4].ForeColor = Color.Blue;
                            collection[i].SubItems[4].Text      = "上传完成";
                            collection[i].SubItems[3].Text      = "100%";
                        }
                    }
                    else
                    {
                        //修改客户端界面数据效果
                        collection[i].Selected              = false;
                        collection[i].BackColor             = Color.Red;
                        collection[i].SubItems[4].ForeColor = Color.Blue;
                        collection[i].SubItems[4].Text      = "跳过";
                        collection[i].SubItems[3].Text      = "100%";
                    }
                }
                button1.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                button1.Enabled = true;
            }
        }