Uploading image in folder Entity frame work

protected void btnSave_Click(object sender, EventArgs e)
    {
        KeetonModel.KeetonEntities context = new KeetonModel.KeetonEntities();
        if (!Page.IsValid)
            return;
        try
        {

            tblListPosition tblpos;
            if (listPostid > 0)
            {
                tblpos = context.tblListPositions.Where(c => c.ListPosingId == listPostid).FirstOrDefault();
                if (tblpos == null)
                {
                    tblpos = new tblListPosition();
                    context.AddTotblListPositions(tblpos);
                }
            }
            else
            {
                tblpos = new tblListPosition();
                context.AddTotblListPositions(tblpos);
                tblpos.ImageUrl = "";
                tblpos.MainImage = "";
                tblpos.BannerImage = "";
                tblpos.ThumnilImage = "";

            }
            tblpos.Date = Convert.ToDateTime(txtDate.Text);
            tblpos.PostingType = ddlPostType.SelectedValue;
            if (ddlPostType.SelectedValue != "Artist")
            {
                tblpos.ArtistType = null;
            }
            else
                tblpos.ArtistType = ddlArtistType.SelectedValue;

            tblpos.Title = txtTitle.Text;
            tblpos.Subtiltle = txtSustitle.Text;
            tblpos.Body = elm2.InnerHtml;
            tblpos.PostedBy = txtPostedBy.Text;
            tblpos.PublishPosting = ChkPublishPosting.Checked;
            tblpos.BannerStatus = ChkBanner.Checked;
            context.SaveChanges();

            string fileName = "";
            string fileNameBig = "";

            string fileMainName = "";
            string fileMainbig = "";
          
            string fileMusicName = "";

            if (fuMusic.HasFile)
            {

                string Apath = Server.MapPath("~/ArtistMusic");
                if (!Directory.Exists(Apath))
                    Directory.CreateDirectory(Apath);

                fileMusicName = Guid.NewGuid().GetHashCode().ToString("x") + fuMusic.PostedFile.FileName;
                fileMusicName = listPostid + "_" + fileMusicName;
                fuMusic.PostedFile.SaveAs(Apath + "\\" + fileMusicName);

            }
            if (!string.IsNullOrEmpty(fileMusicName))
            {
                //tblpos = context.tblListPosition.Where(c => c.ListPosingId == tblpos.ListPosingId).FirstOrDefault();
                //if (tblpos != null)
                //{
                tblpos.Artistmusic = fileMusicName;
                context.SaveChanges();
                //}
            }

            if (FuMainimage.HasFile)
            {

                string Apath = Server.MapPath("~/ListPostingImage");
                if (!Directory.Exists(Apath))
                    Directory.CreateDirectory(Apath);

                fileMainName = Guid.NewGuid().GetHashCode().ToString("x") + FuMainimage.PostedFile.FileName;
                fileMainbig = listPostid + "_" + fileMainName;
                FuMainimage.PostedFile.SaveAs(Apath + "\\" + fileMainbig);

            }
            if (!string.IsNullOrEmpty(fileMainbig))
            {
                //tblpos = context.tblListPosition.Where(c => c.ListPosingId == tblpos.ListPosingId).FirstOrDefault();
                //if (tblpos != null)
                //{
                tblpos.MainImage = fileMainbig;
                context.SaveChanges();
                //}
            }

            if (fuImage.HasFile)
            {

                string Apath = Server.MapPath("~/ListPostingImage");
                if (!Directory.Exists(Apath))
                    Directory.CreateDirectory(Apath);

                fileName = Guid.NewGuid().GetHashCode().ToString("x") + fuImage.PostedFile.FileName;
                fileNameBig = listPostid + "_" + fileName;
                fuImage.PostedFile.SaveAs(Apath + "\\" + fileNameBig);

            }
            if (!string.IsNullOrEmpty(fileNameBig))
            {
                //tblpos = context.tblListPosition.Where(c => c.ListPosingId == tblpos.ListPosingId).FirstOrDefault();
                //if (tblpos != null)
                //{
                tblpos.ImageUrl = fileNameBig;
                context.SaveChanges();
                //}
            }



            if (fuHomeBanner.HasFile)
            {

                string Apath = Server.MapPath("~/HomeBanner");
                if (!Directory.Exists(Apath))
                    Directory.CreateDirectory(Apath);

                fileMainName = Guid.NewGuid().GetHashCode().ToString("x") + fuHomeBanner.PostedFile.FileName;
                fileMainbig = listPostid + "_" + fileMainName;
                fuHomeBanner.PostedFile.SaveAs(Apath + "\\" + fileMainbig);

            }
            if (!string.IsNullOrEmpty(fileMainbig))
            {
                //tblpos = context.tblListPosition.Where(c => c.ListPosingId == tblpos.ListPosingId).FirstOrDefault();
                //if (tblpos != null)
                //{
                tblpos.BannerImage = fileMainbig;
                context.SaveChanges();
                //}
            }

            if (fuHomeThumb.HasFile)
            {

                string Apath = Server.MapPath("~/HomeBanner");
                if (!Directory.Exists(Apath))
                    Directory.CreateDirectory(Apath);

                fileMainName = Guid.NewGuid().GetHashCode().ToString("x") + fuHomeThumb.PostedFile.FileName;
                fileMainbig = listPostid + "_" + fileMainName;
                fuHomeThumb.PostedFile.SaveAs(Apath + "\\" + fileMainbig);

            }
            if (!string.IsNullOrEmpty(fileMainbig))
            {
                //tblpos = context.tblListPosition.Where(c => c.ListPosingId == tblpos.ListPosingId).FirstOrDefault();
                //if (tblpos != null)
                //{
                tblpos.ThumnilImage = fileMainbig;
                context.SaveChanges();
                //}
            }

            //onLoadMethods.Append("self.parent.ClosePopupCallBack();");
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "onLoadMsg", onLoadMethods.ToString(), true);

            Response.Redirect("ListPostings.aspx");
        }
        catch { }

    }

No comments:

Post a Comment