Beispiel #1
0
        /// <summary>
        /// 使用分组上传文件
        /// </summary>
        /// <param name="o">文件的绝对路径</param>
        private void UpLoad_file(object o, BackgroundWorker worker, DoWorkEventArgs e)
        {
            if (worker.CancellationPending)                                     // 如果取消了线程
            {
                e.Cancel = true;
            }
            else
            {
                string   fullname   = o as string;                                          // 文件的绝对路径
                FileInfo file       = new FileInfo(fullname);                               // 文件对象
                long     fileLength = file.Length;                                          // 文件的长度
                string   name       = file.Name;                                            // 文件的名字

                byte[] buffer     = new byte[maxSize];                                      // 每次分组的缓冲区
                int    readLength = 0;                                                      // 每次读取文件的长度
                long   sendLength = 0;                                                      // 总共已发送的长度

                processBarDiage.setLabel1($"\"{name}\"正在上传中");
                Sendbtn.Enabled    = false;
                ConnectBtn.Enabled = false;
                downBtn.Enabled    = false;
                exitBtn.Enabled    = false;

                try
                {
                    FileStream fs = new FileStream(fullname, FileMode.Open, FileAccess.Read); // 文件读写对象
                                                                                              // 文件信息处理
                    List <byte> first = new List <byte>();
                    first.Add(1);                                                             // 标记位
                    string infor = string.Format("{0}*{1}", name, fileLength);                // 文件名*文件长度
                    byte[] temp  = Encoding.UTF8.GetBytes(infor);                             // 使用utf8编码字节数组,长度会边长
                    first.Add(Convert.ToByte(temp.Length));                                   // 文件信息的长度,用于标记
                    first.AddRange(temp);                                                     // 加入文件信息

                    byte[] newbuffer = new byte[maxSize - first.Count];                       // 第一个分组能发送的文件内容的大小的缓冲区
                    readLength = fs.Read(newbuffer, 0, newbuffer.Length);
                    first.AddRange(newbuffer);

                    client.Send(first.ToArray(), 0, first.Count, SocketFlags.None);             // 发送第一个分组
                    sendLength += readLength;
                    precentChange(sendLength, fileLength, worker);                              // 更新进度

                    // 剩余的文件内容
                    while ((readLength = fs.Read(buffer, 0, buffer.Length)) > 0 && sendLength < fileLength)
                    {
                        if (worker.CancellationPending)                                         // 如果取消了线程
                        {
                            e.Cancel = true;
                            CloseServer();                                                      // 断开
                            ConnectServer();                                                    // 重连;
                            break;
                        }
                        client.Send(buffer, 0, readLength, SocketFlags.None);
                        sendLength += readLength;
                        precentChange(sendLength, fileLength, worker);                          // 更新进度
                    }

                    fs.Close();                                                                 // 关闭文件操作流
                    this.processBarDiage.Close();
                }
                catch (Exception i)
                {
                    e.Result = i;
                }
            }
        }