예제 #1
0
        /// <summary>
        /// 发送文件
        /// </summary>
        /// <param name="fileName"></param>
        private void sendFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            var ext     = fileName.Substring(fileName.LastIndexOf('.') + 1);
            var message = new NIMFileMessage
            {
                SessionType    = NIMSessionType.kNIMSessionTypeP2P,
                ReceiverID     = target.accid,
                LocalFilePath  = fileName,
                FileAttachment = new NIMMessageAttachment {
                    DisplayName = fileName, FileExtension = ext
                }
            };

            var body = new FileMessage {
                attach = Util.serialize(new Attach {
                    name = fileName
                })
            };
            var id = sendMessage(6, body);

            TalkAPI.SendMessage(message, (uploaded, total, obj) =>
            {
                void action()
                {
                    mlcMessage.setPosition(id, (int)(100 * uploaded / total));
                    if (uploaded == total)
                    {
                        Messages.showMessage("文件上传完成");
                    }
                }

                while (!(Parent?.IsHandleCreated ?? false))
                {
                    Thread.Sleep(100);
                }

                Invoke((Action)action);
            });
        }
예제 #2
0
        void SendFile(string path)
        {
            string fileName  = System.IO.Path.GetFileNameWithoutExtension(path);
            string extension = System.IO.Path.GetExtension(path);

            if (IsImageFile(path))
            {
                NIM.NIMImageMessage imageMsg = new NIMImageMessage();
                imageMsg.LocalFilePath                 = path;
                imageMsg.ImageAttachment               = new NIMImageAttachment();
                imageMsg.ImageAttachment.DisplayName   = fileName;
                imageMsg.ImageAttachment.FileExtension = extension;
                imageMsg.ReceiverID  = _peerId;
                imageMsg.SessionType = _sessionType;
                using (var i = Image.FromFile(path))
                {
                    imageMsg.ImageAttachment.Height = i.Height;
                    imageMsg.ImageAttachment.Width  = i.Width;
                }
                TalkAPI.SendMessage(imageMsg);
            }
            else
            {
                NIM.NIMFileMessage fileMsg = new NIMFileMessage();
                fileMsg.LocalFilePath                = path;
                fileMsg.FileAttachment               = new NIMMessageAttachment();
                fileMsg.FileAttachment.DisplayName   = fileName;
                fileMsg.FileAttachment.FileExtension = extension;
                fileMsg.ReceiverID  = _peerId;
                fileMsg.SessionType = _sessionType;
                NIM.TalkAPI.SendMessage(fileMsg, (uploaded, total, obj) =>
                {
                    OutputForm.Instance.SetOutput(string.Format("upload file:{0} {1}/{2}", path, uploaded, total));
                });
            }
        }