Example #1
0
        public ActionResult postData()
        {
            ////先把前端传回来的content内容保存为静态页面
            byte[] byteData = new byte[Request.InputStream.Length]; //定义一个字节数组保存前端传回来的Post数据全部数据
            Request.InputStream.Read(byteData, 0, byteData.Length); //将流读取到byteData,InputStream读取到的是http头里的主体数据
            //string postData = System.Text.Encoding.Default.GetString(byteData);//系统的默认编码为gb2312,不适用这种
            string postData = System.Text.Encoding.UTF8.GetString(byteData);

            postData = Server.UrlDecode(postData);                                        //对数据进行url解码
            string[] datas       = postData.Split('&');                                   //对postData数据进行分割,提取出发帖内容里的html数据
            string   contentData = datas[1].ToString();                                   //data[1]为变量名为content的内容

            contentData = contentData.Substring(contentData.IndexOf('=') + 1);            //去除变量名,如content=aaa,只取出aaa
            DateTime datetime     = DateTime.Now;
            string   fileName     = datetime.ToString("yyyyMMddHHmmss_ffff") + ".html";   //定义文件名fileName
            string   fileNamePath = Server.MapPath("~/StaticHtml/TieZiHtml/") + fileName; //物理路径

            while (System.IO.File.Exists(fileNamePath))                                   //先判断文件是否存在,若存在:更换文件名
            {
                datetime     = DateTime.Now;
                fileName     = datetime.ToString("yyyyMMddHHmmss_ffff") + ".html";
                fileNamePath = Server.MapPath("~/StaticHtml/TieZiHtml/") + fileName;
            }
            System.IO.FileStream fs = new System.IO.FileStream(fileNamePath, System.IO.FileMode.Create);
            byte[] contentBytes     = System.Text.Encoding.UTF8.GetBytes(contentData);
            //byte[] contentBytes = System.Text.Encoding.Default.GetBytes(contentData);
            fs.Write(contentBytes, 0, contentBytes.Length);
            fs.Close();//保存静态html成功
            ///////将帖子数据保存到数据库
            Post_bll   Post           = new Post_bll();
            Theme_bll  themeName      = new Theme_bll();
            People_bll User           = new People_bll();
            string     caption        = Request["question"].ToString();
            string     userName       = Session["userName"].ToString();
            int        userId         = User.GainUserId(userName);
            string     theme          = Request["theme"].ToString();
            int        themeId        = themeName.CollocateThemeId(theme);
            string     staticHtmlPath = "/StaticHtml/TieZiHtml/" + fileName; //相对路径
            string     editorContent  = datas[3].ToString();                 //data[3]的为前端传回来的发帖内容的纯文本

            editorContent = editorContent.Substring(editorContent.IndexOf('=') + 1);
            System.Text.RegularExpressions.Regex regexImg = new System.Text.RegularExpressions.Regex(@"<img[^>]+>");
            editorContent = regexImg.Replace(editorContent, "");//过滤掉editorContent里图片
            int len = editorContent.Length;

            if (len > 200)//如果editorContent的长度超过200,取editorContent里的前两百个字符,将用于保存到数据库
            {
                len = 200;
            }

            string content200 = editorContent.Substring(0, len);

            if (Post.finshInsert(caption, content200, userId, themeId, staticHtmlPath, datetime) != 1)
            {
                return(Content("保存帖子信息时数据库出错"));
            }//将帖子数据保存到数据库---------成功
            //////获取所有图片里的图片路径,并且将图片路径保存到数据库里
            System.Text.RegularExpressions.Regex           regImg2 = new System.Text.RegularExpressions.Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);// 定义正则表达式用来匹配 img 标签
            System.Text.RegularExpressions.MatchCollection matches = regImg2.Matches(contentData);
            int i = 0;

            string[] strUrlList = new string[matches.Count];
            foreach (System.Text.RegularExpressions.Match match in matches)
            {
                strUrlList[i++] = match.Groups["imgUrl"].Value;// 取得匹配项列表
            }
            if (strUrlList.Length >= 1)
            {
                int           postId        = Post.GetPostId(caption); //根据帖子的标题查询数据库,得到该贴子的postId
                PostPhoto_bll postPhoto_bll = new PostPhoto_bll();
                if (postPhoto_bll.InsertPhotoUrl(postId, strUrlList, 1) < 0)
                {
                    return(Content("保存图片路径时数据库出错"));
                }
            }
            return(Content("成功"));
        }
Example #2
0
        /// <summary>
        /// 对主贴的回帖
        /// </summary>
        /// <returns></returns>
        public ActionResult MakePostReply()
        {
            ////--------先把前端传回来的content内容保存为静态页面
            byte[] byteData = new byte[Request.InputStream.Length]; //定义一个字节数组保存前端传回来的Post数据全部数据
            Request.InputStream.Read(byteData, 0, byteData.Length); //将流读取到byteData,InputStream读取到的是http头里的主体数据
            //string postData = System.Text.Encoding.Default.GetString(byteData);//系统的默认编码为gb2312,不适用这种
            string postData = System.Text.Encoding.UTF8.GetString(byteData);

            postData = Server.UrlDecode(postData);                                         //对数据进行url解码
            string[] datas       = postData.Split('&');                                    //对前端传回来的数据进行分割,提取出文本框里的html数据
            string   contentData = datas[0].ToString();                                    //data[0]为变量名为content的内容

            contentData = contentData.Substring(contentData.IndexOf('=') + 1);             //去除变量名,如content=aaa,只取出aaa
            DateTime datetime     = DateTime.Now;
            string   fileName     = datetime.ToString("yyyyMMddHHmmss_ffff") + ".html";    //定义文件名fileName
            string   fileNamePath = Server.MapPath("~/StaticHtml/HuiTieHtml/") + fileName; //物理路径

            while (System.IO.File.Exists(fileNamePath))                                    //先判断文件是否存在,若存在:更换文件名
            {
                datetime     = DateTime.Now;
                fileName     = datetime.ToString("yyyyMMddHHmmss_ffff") + ".html";
                fileNamePath = Server.MapPath("~/StaticHtml/HuiTieHtml/") + fileName;
            }
            System.IO.FileStream fs = new System.IO.FileStream(fileNamePath, System.IO.FileMode.Create);
            byte[] contentBytes     = System.Text.Encoding.UTF8.GetBytes(contentData);
            //byte[] contentBytes = System.Text.Encoding.Default.GetBytes(contentData);
            fs.Write(contentBytes, 0, contentBytes.Length);
            fs.Close();//---------保存静态html成功
            ///----------/将数据保存到数据库里tb_ReplyPost (主贴编号 回帖者Id
            ///回帖内容的前200字符 点赞数 日期 回帖的静态html)
            int      postId        = Convert.ToInt32(Request["hidPostId"]); //主贴id
            int      supportCount  = 0;                                     //点赞数
            string   replyUserName = Request["hidHuiTieUserName"].ToString();
            User_bll user_bll      = new User_bll();
            int      replyUserId   = user_bll.GetUserId(replyUserName); //回帖者id
            string   editorContent = datas[3].ToString();               //回帖内容的纯文本包含图片,data[3]的为前端传回来的发帖内容的纯文本 (备注:不能从request["editorContent"]读取,会报错提示说存在html标签)

            editorContent = editorContent.Substring(editorContent.IndexOf('=') + 1);
            System.Text.RegularExpressions.Regex regexImg = new System.Text.RegularExpressions.Regex(@"<img[^>]+>");
            editorContent = regexImg.Replace(editorContent, "");//过滤掉editorContent里图片
            int len = editorContent.Length;

            if (len > 200)//如果editorContent的长度超过200,取editorContent里的前两百个字符,将用于保存到数据库
            {
                len = 200;
            }
            string        content200       = editorContent.Substring(0, len);      //回帖的200字符
            string        staticHuiTieHtml = "/StaticHtml/HuiTieHtml/" + fileName; //相对路径
            ReplyPost_bll replyPost_bll    = new ReplyPost_bll();

            replyPost_bll.InsertReplyPost(postId, replyUserId, content200, supportCount, datetime, staticHuiTieHtml);
            //--------回帖数据保存成功

            //////获取回帖里的所有图片的路径,并且将图片路径保存到数据库里tb_PostPhoto (PostPhoto_PostType=)
            int replyPostId = replyPost_bll.GetReplyPostId(staticHuiTieHtml);                                                                                                                                                                                                          //根据回帖的静态html路径查询数据库,得到该贴子的replyPost_Id

            System.Text.RegularExpressions.Regex           regImg2 = new System.Text.RegularExpressions.Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); // 定义正则表达式用来匹配 img 标签
            System.Text.RegularExpressions.MatchCollection matches = regImg2.Matches(contentData);
            int i = 0;

            string[] strUrlList = new string[matches.Count];
            foreach (System.Text.RegularExpressions.Match match in matches)
            {
                strUrlList[i++] = match.Groups["imgUrl"].Value;// 取得匹配项列表
            }
            if (strUrlList.Length >= 1)
            {
                PostPhoto_bll postPhoto_bll = new PostPhoto_bll();
                if (postPhoto_bll.InsertPhotoUrl(replyPostId, strUrlList, 2) < 0)
                {
                    return(Content("保存图片路径时数据库出错"));
                }
            }
            return(RedirectToAction("../Topic/PostDetails", new { postId = postId }));
        }