/// <summary> /// 接收客户端文件数据。 /// </summary> /// <param name="data"></param> protected virtual void ReceiveClientData(UploadFileMSG data) { if (data != null) { string teacherId = this.userInfo.UserID, classId = this.sci.ClassInfo.ClassID; WorkStoreHelper.WorkAddQueueEntity entity = new WorkStoreHelper.WorkAddQueueEntity(teacherId, classId, data); entity.Changed += new RaiseChangedHandler(delegate(string content) { this.RaiseChanged(content); }); entity.UpdateControls += new UpdateStudentControlsHandler(delegate(StudentEx stuEx) { this.OnUpdateControls(stuEx); this.RaiseChanged(string.Format("[{0},{1}]更新状态,队列处理完毕!", stuEx.StudentName, stuEx.StudentCode)); }); WorkStoreHelper.AddWorkToQueueStore(entity); this.RaiseChanged(string.Format("[{0},{1},{2}]上传作业已接收并进入队列处理...", data.Student.StudentName, data.Student.StudentCode, data.WorkName)); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUpload_Click(object sender, EventArgs e) { string oldbtnText = this.btnUpload.Text; try { if (!this.btnUpload.Enabled) return; this.bUploadComplete = false; if (this.catalog == null) return; this.btnUpload.Text = "开始上传.."; this.btnUpload.Enabled = false; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(object o) { UploadFileMSG msg = new UploadFileMSG(); msg.CatalogID = this.catalog.CatalogID; msg.Student = this.CoreService["student"] as Student; if (msg.Student != null) msg.UID = msg.Student.StudentID; msg.WorkID = Guid.NewGuid().ToString().Replace("-", ""); msg.WorkName = this.txtWorkName.Text.Trim(); this.OnClearErrorEvent(); if (string.IsNullOrEmpty(msg.WorkName)) { this.OnSetErrorEvent(this.txtWorkName, "作品名称不能为空!"); return; } this.OnMessageEvent(MessageType.Normal, "开始上传数据准备..."); msg.WorkType = this.chkPublic.Checked ? EnumWorkType.Public : EnumWorkType.Protected; msg.Description = this.txtDescription.Text; msg.Time = DateTime.Now; if (this.fileUploadItems == null || this.fileUploadItems.Count == 0) { this.OnMessageEvent(MessageType.Normal | MessageType.PopupInfo, "未选择上传的作品附件!"); return; } msg.Files = new StudentWorkFiles(); #region 装载文件数据。 string path = null; for (int i = 0; i < this.fileUploadItems.Count; i++) { if (File.Exists(path = this.fileUploadItems[i].Path)) { StudentWorkFile swf = new StudentWorkFile(); swf.FileID = Guid.NewGuid().ToString().Replace("-", ""); swf.FileName = this.fileUploadItems[i].FileName; swf.FileExt = this.fileUploadItems[i].Ext; swf.ContentType = "application/octet-stream"; using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] buf = new byte[512]; swf.Size = stream.Length; using (MemoryStream ms = new MemoryStream()) { int len = 0; while ((len = stream.Read(buf, 0, buf.Length)) > 0) { ms.Write(buf, 0, len); } swf.Data = ms.ToArray(); } } msg.Files.Add(swf); } } #endregion PortSettings ports = this.CoreService["portsettings"] as PortSettings; IPAddress hostIP = IPAddress.Parse(string.Format("{0}", this.CoreService["host_ip"])); this.OnMessageEvent(MessageType.Normal, "上传数据准备完成,开始连接教师机主机..."); bool result = false; using (TcpClientService tcs = new WorkUpTcpClient(new IPEndPoint(hostIP, ports.FileUpTransfer), ports.MaxFileSize)) { tcs.Changed += new Yaesoft.SFIT.Client.RaiseChangedHandler(delegate(string content) { this.OnMessageEvent(MessageType.Normal, content); }); result = tcs.Upload(msg); } System.Threading.Thread.Sleep(700); this.ThreadSafeMethod(new MethodInvoker(delegate() { if (result) this.DialogResult = DialogResult.Yes; })); this.OnMessageEvent(MessageType.Normal | MessageType.PopupInfo, result ? "作品上传成功" : "作品上传失败,请重新上传!"); this.bUploadComplete = true; this.ThreadSafeMethod(new MethodInvoker(delegate() { this.btnUpload.Text = oldbtnText; this.btnUpload.Enabled = true; })); })); } catch (Exception x) { this.OnMessageEvent(MessageType.Normal | MessageType.PopupWarn, "系统异常:" + x.Message); this.btnUpload.Text = oldbtnText; this.btnUpload.Enabled = true; } }