コード例 #1
0
        /// <summary>
        ///     接受发送给特定人的文件
        /// </summary>
        /// <param name="msgHandler"></param>
        private void ReceiveFileForIndividual(FileHandler msgHandler)
        {
            byte[] fileStream = Encoding.UTF8.GetBytes(msgHandler.message);
            //保存文件对话框对象
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.DefaultExt = msgHandler.fileType;
            if (msgHandler.fileType.Equals("txt"))
                sfd.Filter = "文本文件(*.txt)|*.txt";
            else if (msgHandler.fileType.Equals("doc"))
                sfd.Filter = "word文档(*.doc)|*.doc";
            else if (msgHandler.fileType.Equals("docx"))
                sfd.Filter = "word文档(*.docx)|*.docx";
            else if (msgHandler.fileType.Equals("cpp"))
                sfd.Filter = "c++文件(*.cpp)|*.cpp";
            else if (msgHandler.fileType.Equals("c"))
                sfd.Filter = "c文件(*.c)|*.c";
            else if (msgHandler.fileType.Equals("h"))
                sfd.Filter = "头文件(*.h)|*.h";
            else if (msgHandler.fileType.Equals("ppt"))
                sfd.Filter = "PowerPoint文件(*.ppt)|*.ppt";
            else if (msgHandler.fileType.Equals("pptx"))
                sfd.Filter = "PowerPoint文件(*.pptx)|*.pptx";
            else if (msgHandler.fileType.Equals("pdf"))
                sfd.Filter = "PDF文档(*.pdf)|*.pdf";
            else if (msgHandler.fileType.Equals("xls"))
                sfd.Filter = "excel文档(*.xls)|*.xls";
            if (sfd.Filter == null || sfd.Filter.Equals(""))
                sfd.Filter += "All FIles(*.*)|*.*";
            else
                sfd.Filter += "| All FIles(*.*)|*.*";

            List<Paragraph> userParagraphList = dictParagraph[msgHandler.from];

            Paragraph newParagraph = new Paragraph();

            Run run = new Run()
            {
                Text = System.DateTime.Now.ToString() + " 收到来自" + msgHandler.fromName + "(" + msgHandler.from + ")" + " 的文件\r\n",
                Foreground = new SolidColorBrush(Colors.SteelBlue)
            };
            newParagraph.Inlines.Add(run);

            userParagraphList.Add(newParagraph);

            onlineFriendType sendTo = (onlineFriendType)onlineFriendlistListBox.SelectedItem;

            sfd.FileName = msgHandler.fileName + "." + msgHandler.fileType;
            if (sfd.ShowDialog(this) == true)
            {
                //获取文件将要保存的路径
                string fileSavePath = sfd.FileName;
                //创建文件流,让文件流根据路径创建一个文件
                using (FileStream fs = new FileStream(fileSavePath, FileMode.Create))
                {
                    Run mrun = new Run()
                    {
                        Text = "文件保存到" + fileSavePath + "\r\n",
                        Foreground = new SolidColorBrush(Colors.SteelBlue)
                    };
                    newParagraph.Inlines.Add(mrun);
                    fs.Write(fileStream, 1, fileStream.Length - 1);
                }
            }

            if (sendTo.nameAndId.Equals("所有人") == false && sendTo.userData.userId.Equals(msgHandler.from))
            {
                chatRichTextBox.Document.Blocks.Add(newParagraph);
            }
            else
            {
                int index;
                if (FindItemByUserId(msgHandler.from, out index))
                {
                    onlineFriendType user = (onlineFriendType)onlineFriendlistListBox.Items[index];
                    user.increaseNum();
                }
            }
        }
コード例 #2
0
        /// <summary>
        ///     发送文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sendFile_Click(object sender, RoutedEventArgs e)
        {
            onlineFriendType sendTo = (onlineFriendType)onlineFriendlistListBox.SelectedItem;

            //用文件流打开用户选择的文件
            try
            {
                using (FileStream fs = new FileStream(fileTextBox.Text, FileMode.Open))
                {
                    //定义一个4M的数组(缓冲区)
                    byte[] arrFile = new byte[1024 * 1024 * 2];
                    //用于发送真实数据的数组
                    byte[] arrFileSend = new byte[1];
                    //将文件数据读到数组arrFile中,并获取文件的真实长度
                    int length = fs.Read(arrFile, 0, arrFile.Length);

                    string fileStream = Encoding.UTF8.GetString(arrFile, 0, length);

                    int indexPoint = fileTextBox.Text.LastIndexOf('.');
                    int indexLine = fileTextBox.Text.LastIndexOf('\\');

                    string fileName = fileTextBox.Text.Substring(indexLine + 1, indexPoint - indexLine - 1);
                    string fileType = fileTextBox.Text.Substring(indexPoint + 1, fileTextBox.Text.Length - indexPoint - 1);

                    if (sendTo.nameAndId.Equals("所有人"))
                    {
                        FileHandler msgHandler = new FileHandler(userData.userId, userData.userName, fileName, fileType, "", fileStream);
                        string sendMsg = JsonConvert.SerializeObject(msgHandler);
                        byte[] arrTmp = Encoding.UTF8.GetBytes(sendMsg);
                        arrFileSend = new byte[arrTmp.Length + 1];
                        arrFileSend[0] = FILE_TO_ALL;
                        Buffer.BlockCopy(arrTmp, 0, arrFileSend, 1, arrTmp.Length);
                    }
                    else
                    {
                        FileHandler msgHandler = new FileHandler(userData.userId, userData.userName, fileName, fileType, sendTo.userData.userId, fileStream);
                        string sendMsg = JsonConvert.SerializeObject(msgHandler);
                        byte[] arrTmp = Encoding.UTF8.GetBytes(sendMsg);
                        arrFileSend = new byte[arrTmp.Length + 1];

                        arrFileSend[0] = FILE_TO_INDIVIDUAL;

                        Buffer.BlockCopy(arrTmp, 0, arrFileSend, 1, arrTmp.Length);
                    }

                    List<Paragraph> userParagraphList;

                    if (sendTo.nameAndId.Equals("所有人"))
                        userParagraphList = dictParagraph["AllUsers"];
                    else
                        userParagraphList = dictParagraph[sendTo.userData.userId];

                    Paragraph newParagraph = new Paragraph();

                    Run run = new Run()
                    {
                        Text = System.DateTime.Now.ToString() + " 发送文件" + fileName + "." + fileType + "\r\n",
                        Foreground = new SolidColorBrush(Colors.SteelBlue)
                    };
                    newParagraph.Inlines.Add(run);

                    userParagraphList.Add(newParagraph);

                    chatRichTextBox.Document.Blocks.Add(newParagraph);

                    try
                    {
                        socketClient.Send(arrFileSend);
                        fileTextBox.Text = "";
                    }
                    catch (SocketException se)
                    {
                        Console.WriteLine("【错误】发送消息异常:" + se.Message);
                        return;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("【错误】发送消息异常:" + ex.Message);
                        return;
                    }
                }
            }
            catch
            {
                MessageBox.Show("请选择正确的文件路径!");
            }
        }