Ejemplo n.º 1
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string             strTitle   = this.txtTitle.Text;
            string             strAuthor  = this.txtAuthor.Text;
            string             strClick   = this.txtClick.Text;
            string             strSource  = this.txtSource.Text;
            string             strType    = this.ddlType.SelectedValue;
            string             strContent = this.fckContent.Text;
            HttpFileCollection thefiles   = Request.Files;

            bool IsAddImgToDb = this.chkIsAddImgToDb.Checked;

            if (strType == "0" || strType == "")
            {
                this.lblTypeMessage.Text = "请选择文章分类";
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", "alert(\"请选择文章分类!\")", true);
                return;
            }

            //检查所有文件的后缀名是否为允许上传的类型
            for (int ifile = 0; ifile < thefiles.Count; ifile++)
            {
                HttpPostedFile file     = thefiles[ifile];
                string         fileName = file.FileName.ToLower();
                if (fileName != "" && file.ContentLength != 0)
                {
                    string fileExtension = System.IO.Path.GetExtension(fileName);    //扩展名
                    fileName = System.IO.Path.GetFileNameWithoutExtension(fileName); //文件名
                    bool     AllowUpload = false;
                    string   strFileType = System.Configuration.ConfigurationManager.AppSettings["AllowFileTypes"].ToString();
                    string[] filetypes   = strFileType.Split(',');

                    for (int i = 0; i < filetypes.Length; i++)
                    {
                        if (fileExtension == "." + filetypes[i].ToLower())
                        {
                            AllowUpload = true; break;
                        }
                    }
                    if (!AllowUpload)
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "alertfileNotAllow", "alert('您上传的附件中含有不允许上传的附件类型,请重新添加')", true);
                        return;
                    }
                }
            }

            string uploadFileNames = string.Empty;

            for (int ifile = 0; ifile < thefiles.Count; ifile++)
            {
                HttpPostedFile file     = thefiles[ifile];
                string         fileName = file.FileName;
                if (file.ContentLength != 0)
                {
                    fileName = fileName.ToLower();
                    string fileExtension = System.IO.Path.GetExtension(fileName);    //扩展名
                    fileName = System.IO.Path.GetFileNameWithoutExtension(fileName); //文件名
                    //fileName = fileName.Substring(fileName.LastIndexOf('\\'), fileName.LastIndexOf('.')); //文件名

                    //判断文件是否已经存在,如果存在则重命名,直到文件名不存在
                    string fileRename = fileName + fileExtension;
                    int    tryTimes   = 0;
                    while (File.Exists(Server.MapPath("../../") + "uploads/" + fileRename))
                    {
                        tryTimes++;
                        fileRename = fileName + "(" + tryTimes.ToString() + ")" + fileExtension;
                    }

                    uploadFileNames += (uploadFileNames == string.Empty) ? fileRename : "/" + fileRename; //多个文件用符号‘/’隔开

                    string fullFileName = Server.MapPath("../../") + "uploads/" + fileRename;
                    file.SaveAs(fullFileName);
                }
            }

            string oldFujian = Request.Form["_fujianOld"];

            if (oldFujian != "")
            {
                uploadFileNames += (uploadFileNames == string.Empty) ? oldFujian : "/" + oldFujian; //多个文件用符号‘/’隔开
            }


            string[] strSrcs = getPics(ref strContent);
            this.fckContent.Text = strContent;
            int articleId;

            if (theState == MyPageState.add)
            {
                articleId = bllarticle.Add_ReturnID(new Model.Article {
                    dbo_Title = strTitle, dbo_Typeid = Convert.ToInt32(strType), dbo_Content = strContent, dbo_Author = strAuthor, dbo_Source = strSource, dbo_Click = Convert.ToInt32(strClick), dbo_Fujian = uploadFileNames, dbo_IsLock = this.chkIsLock.Checked, dbo_IsTop = this.chkIsTop.Checked, dbo_Ptime = DateTime.Now
                });

                //只添加一张图片到数据库中
                if (strSrcs.Length > 0 && IsAddImgToDb)
                {
                    bllarticleimg.Add(new Model.ArticleImg {
                        dbo_ImgSrc = strSrcs[0], dbo_articleId = articleId, dbo_ImgBigSrc = strSrcs[0], dbo_SpecialityId = Convert.ToInt32(Request.QueryString["spcId"]), dbo_ImgArt = strTitle
                    });
                }
            }
            else
            {
                articleId                = Convert.ToInt32(Request.QueryString["id"]);
                modelarticle             = bllarticle.GetModel(articleId);
                modelarticle.dbo_Title   = strTitle;
                modelarticle.dbo_Content = strContent;
                modelarticle.dbo_Author  = strAuthor;
                modelarticle.dbo_Source  = strSource;
                modelarticle.dbo_Click   = strClick == ""?0:Convert.ToInt32(strClick);
                modelarticle.dbo_Fujian  = uploadFileNames;
                modelarticle.dbo_IsLock  = this.chkIsLock.Checked;
                modelarticle.dbo_IsTop   = this.chkIsTop.Checked;
                modelarticle.dbo_Ptime   = DateTime.Now;
                bllarticle.Update(modelarticle);
                bllarticleimg.DeleteByArticleId(articleId);

                //只添加一张图片到数据库中
                if (strSrcs.Length > 0 && IsAddImgToDb)
                {
                    bllarticleimg.Add(new Model.ArticleImg {
                        dbo_ImgSrc = strSrcs[0], dbo_articleId = articleId, dbo_ImgBigSrc = strSrcs[0], dbo_SpecialityId = Convert.ToInt32(Request.QueryString["spcId"]), dbo_ImgArt = strTitle
                    });
                }
            }

            string spcId = "0";

            if (Request.QueryString["spcId"] != null)
            {
                spcId = Request.QueryString["spcId"].ToString();
            }

            if (sys.IsStatic)
            {
                string strDir = sys.getSpcDir(Convert.ToInt32(spcId));
                sys.ToStaticListWithArticleEdit(Convert.ToInt32(this.ddlType.SelectedValue), strDir);
                sys.ToStaticContent(strDir, articleId, DateTime.Now);
            }
            else //判断是否需要生成首页
            {
                sys.ToStaticPageWithActive(Convert.ToInt32(spcId));
            }

            Response.Redirect("articleList.aspx?spcId=" + spcId);
        }