本帖最后由 王针 于 2012-6-1 15:52 编辑
我在做那个聊天室的程序,发现我跟视频里代码一样(就变量命名变了一下而已),可是还是无法弹出文件另存为的窗口,到底是为什么啊?- #region 客户端发送文件
- private void btnFileSend_Click(object sender, EventArgs e)
- {
- using (FileStream fs = new FileStream(txtFileBrowse.Text, FileMode.Open))
- {
- byte[] fileSave = new byte[1024 * 1024 * 2];
- int length = fs.Read(fileSave, 0, fileSave.Length);//daikaolv
- byte[] fileToSend = new byte[length + 1];
- fileToSend[0] = 1;//1表示文件
- Buffer.BlockCopy(fileSave, 0, fileToSend, 1, length);
- sokConnection.Send(fileToSend);
- ShowMsg("发送了文件:【" + Path.GetFileName(txtFileBrowse.Text) + "】");
- }
- }
- #endregion
复制代码- void RcvMsg(object sokConnection)
- {
- Socket sok = sokConnection as Socket;
- while (true)
- {
- byte[] msgToRcv = new byte[1024 * 1024 * 2];
- int length = sok.Receive(msgToRcv);
- if (0 == msgToRcv[0])
- {
- string strMsg = System.Text.Encoding.Default.GetString(msgToRcv, 1, length - 1);
- ShowMsg("收到信息:【" + strMsg + "】");
- }
- else if(1 == msgToRcv[0])
- {
- SaveFileDialog sfd=new SaveFileDialog();
- if (sfd.ShowDialog() == DialogResult.OK)
- {
- using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
- {
- fs.Write(msgToRcv, 1, length - 1);
- ShowMsg("保存成功:" + sfd.FileName);
- }
- }
- }
- }
- }
复制代码 |