예제 #1
0
        public ActionResult Forward(SystemMsg sm, string UserID)
        {
            try
            {
                var            result  = true;
                IList <string> errList = new List <string>();
                //如果原邮件附带附件,则需要重新生成附件信息以区分新旧邮件的附件
                string existAttachIds = Request.Params["existAttachIds"];
                //附件原文件需要复制吗??
                if (!(string.Empty == existAttachIds))
                {
                    foreach (string attachID in existAttachIds.Split(','))
                    {
                        Attachment attach = this.service.Attachment.Get(Convert.ToInt32(attachID));
                        if (attach != null)
                        {
                            //获取原附件的标题(非文件名)及其路径
                            string attachTitle = attach.Title;
                            string attachPath  = attach.FileUrl;
                            string newFileUrl  = string.Empty;
                            //判断原文件是否存在
                            FileInfo fi = new FileInfo(Server.MapPath(attachPath));
                            if (!fi.Exists)//原附件不存在
                            {
                                result = false;
                                errList.Add("原附件:" + attachTitle + " 不存在,已自动删除附件信息,请重新操作");
                                this.service.Attachment.Delete(attach);
                            }
                            else
                            { //存在
                                //设定保存新文件的路径
                                string newFolder = Server.MapPath(_attachmentBaseDir);
                                string todayFmt  = DateTime.Today.ToString("yyyyMM");
                                newFolder = Path.Combine(newFolder, AttachmentType.SysMsg.ToString(), todayFmt);
                                //判断保存新文件所在的文件夹路径是否存在
                                DirectoryInfo di = new DirectoryInfo(newFolder);
                                if (!di.Exists)
                                { //文件夹不存在,创建
                                    di.Create();
                                }
                                //重命名复制后的文件名
                                DateTime dt     = DateTime.Now;
                                DateTime sdt    = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                                int      difdt  = (int)(dt - sdt).TotalSeconds;
                                string   reName = difdt + "_" + attachTitle;
                                string   newFullFileNameForCopy = Path.Combine(newFolder, reName);//新文件完整的复制路径
                                fi.CopyTo(newFullFileNameForCopy);
                                //写入附件信息中的文件相对路径
                                newFileUrl = _attachmentBaseDir + AttachmentType.SysMsg.ToString() + "/" + todayFmt + "/";
                                newFileUrl = VirtualPathUtility.Combine(newFileUrl, reName);
                                newFileUrl = VirtualPathUtility.ToAbsolute(newFileUrl);

                                dynamic newattach = attach.Clone();
                                newattach.ObjectId = sm.ID;
                                newattach.FileUrl  = newFileUrl;
                                this.service.Attachment.Add(newattach);
                            }
                        }
                    }
                }
                if (result)
                {
                    result = this.service.SystemMsg.SendMsg(sm, UserID);
                    if (result)
                    {
                        return(OperateResult(result, Lang.Msg_Operate_Success, result));
                    }
                    else
                    {
                        return(OperateResult(result, Lang.Msg_Operate_Failed, result));
                    }
                }
                else
                {
                    return(OperateResult(result, string.Join("<br/>", errList), null));
                }
            }
            catch (Exception e)
            {
                log.Error(e.Message, e);
                return(ContentResult(false, Lang.Msg_Operate_Failed + e.Message, null));
            }
        }