public bool validateFile(string path) { ValidateImage v = new ValidateImage(); string message; bool check = v.validateFile(path, out message); if (!check) { MessageBox.Show(message); } return(check); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //获得保存了验证码图片的内存 MemoryStream ms = ValidateImage.GenerateImage(); //清空响应 Response.ClearContent(); //设置响应类型 Response.ContentType = "image/Gif"; //返回响应 Response.BinaryWrite(ms.ToArray()); } }
protected void btnSubmit_Click(object sender, EventArgs e) { try { string errMsg = ""; errMsg = validateEntry(); if (!String.IsNullOrEmpty(errMsg)) { lblErrMsg.Text = errMsg.Replace("\n", "<br/>"); AlertMsg(errMsg.Replace("\n", "\\n")); //Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "errMsg", "alert('hi')",true); return; } bool chkflag = true; string folderPath = ConfigurationManager.AppSettings["ImageFolder"].ToString(); string fileNamelogo = "", fileNamead = "", fileNamebig = "", WrTofileName = ""; Int64 PId = 0; DataTable prdPrice = gridViewtoDatatable().Copy(); DataTable prdDescription = ProductDesGridtoDatatable().Copy(); DataTable prdMetaTag = MetaGridtoDatatable().Copy(); //prdPrice.Columns.Remove("Srno"); fileNamelogo = ConfigurationManager.AppSettings["DefaultLogoImg"].ToString(); fileNamead = ConfigurationManager.AppSettings["DefaultAddImg"].ToString(); fileNamebig = ConfigurationManager.AppSettings["DefaultBigImg"].ToString(); ValidateImage objImg = new ValidateImage(); if ((fileuplogo.HasFile) && (fileuplogo.PostedFile.ContentLength > 0)) { objImg.saveimage(fileuplogo, out fileNamelogo); } if ((fileupad.HasFile) && (fileupad.PostedFile.ContentLength > 0)) { objImg.saveimage(fileupad, out fileNamead); } if ((fileuplogobig.HasFile) && (fileuplogobig.PostedFile.ContentLength > 0)) { objImg.saveimage(fileuplogobig, out fileNamebig); } if (String.IsNullOrEmpty(fileNamelogo) || String.IsNullOrEmpty(fileNamead) || String.IsNullOrEmpty(fileNamebig)) { if (File.Exists(Server.MapPath(folderPath + fileNamelogo))) { File.Delete(Server.MapPath(folderPath + fileNamelogo)); } if (File.Exists(Server.MapPath(folderPath + fileNamead))) { File.Delete(Server.MapPath(folderPath + fileNamead)); } if (File.Exists(Server.MapPath(folderPath + fileNamebig))) { File.Delete(Server.MapPath(folderPath + fileNamebig)); } chkflag = false; } if (chkflag) // Images are saved successfuly. { chkflag = false; try { // Try inserting into database. //SqlCommand cmd = new SqlCommand("insert into mproduct(prdiddisplay,categoryId,name,ShortName,description,strength,expirydate,avgdelivery,price,logoPrice,genername,brandname,pageName,logoimgpath,adimgpath,bigimgpath,createdby,createddate,modifiedby,modifieddate,activeflag) values('empty',@categoryId,@name,@ShortName,@description,@strength,@expirydate,@avgdelivery,@price,@logoPrice,@genername,@brandname,@pageName,@logoimgpath,@adimgpath,@bigimgpath,'admin',getdate(),'admin',getdate(),@activeflag)", con); SqlCommand cmd = new SqlCommand("Product_IUD", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@categoryId", ddlsubcategory.SelectedValue); cmd.Parameters.AddWithValue("@name", txtproductname.Text); cmd.Parameters.AddWithValue("@ShortName", txtShortName.Text); cmd.Parameters.AddWithValue("@description", HttpUtility.HtmlEncode(txtproductdesc.Text)); cmd.Parameters.AddWithValue("@strength", txtstrength.Text); cmd.Parameters.AddWithValue("@expirydate", txtexpiry.Text); cmd.Parameters.AddWithValue("@avgdelivery", txtdelivery.Text); cmd.Parameters.AddWithValue("@price", txtprice.Text); cmd.Parameters.AddWithValue("@logoPrice", txtLogoPrice.Text); cmd.Parameters.AddWithValue("@genername", txtgeneric.Text); cmd.Parameters.AddWithValue("@brandname", txtbrand.Text); cmd.Parameters.AddWithValue("@pageName", (txtproductname.Text.Trim().Replace(" ", "-"))); cmd.Parameters.AddWithValue("@logoimgpath", folderPath + fileNamelogo); // folderPath = ~/images/products/ and fileNamelogo = abc.jpg cmd.Parameters.AddWithValue("@adimgpath", folderPath + fileNamead); cmd.Parameters.AddWithValue("@bigimgpath", folderPath + fileNamebig); cmd.Parameters.AddWithValue("@activeflag", radioactive.Checked ? 1 : 0); cmd.Parameters.AddWithValue("@QuerType", "I"); cmd.Parameters.AddWithValue("@LogInUser", UserInfo.GetUserInfo().userId); if ((prdPrice != null) && (prdPrice.Rows.Count > 0)) { cmd.Parameters.AddWithValue("@priceTd", prdPrice); } if ((prdDescription != null) && (prdDescription.Rows.Count > 0)) { cmd.Parameters.AddWithValue("@prdDesTbl", prdDescription); } if ((prdMetaTag != null) && (prdMetaTag.Rows.Count > 0)) { cmd.Parameters.AddWithValue("@prdMetaTagTbl", prdMetaTag); } if (con.State == ConnectionState.Closed) { con.Open(); SqlTrn = con.BeginTransaction(); cmd.Transaction = SqlTrn; } da = new SqlDataAdapter(cmd); ds = new DataSet(); da.Fill(ds); if ((ds != null) && (ds.Tables != null) && (ds.Tables.Count > 0)) { PId = Convert.ToInt64(ds.Tables[0].Rows[0][0]); WrTofileName = ds.Tables[1].Rows[0]["folderName"].ToString(); chkflag = true; } } catch (Exception) { chkflag = false; } //Create Html file //if database insertion was successful and folder found then create html file. /* * if (chkflag && (System.IO.Directory.Exists(Server.MapPath("~\\" + WrTofileName)))) * { * chkflag = false; * try * { * * StreamWriter sw = new StreamWriter(Server.MapPath("~\\" + WrTofileName + "\\" + txtproductname.Text.Trim().Replace(" ", "-") + ".html")); * * string line = ""; * //Pass the file path and file name to the StreamReader constructor * StreamReader sr = new StreamReader(Server.MapPath(ConfigurationManager.AppSettings["ProductHtml"].ToString())); * //Read the first line of text * line = sr.ReadLine(); * //Continue to read until you reach end of file * while (line != null) * { * if (line.Contains("[xxxMetaTagxxx]")) * { * string strMetaName = "", strMetaContent = ""; * foreach (GridViewRow grRow in grdMetaTag.Rows) * { * if (grRow.RowType == DataControlRowType.DataRow) * { * strMetaName = ((Label)grRow.FindControl("lblName")).Text; * strMetaContent = ((Label)grRow.FindControl("lblContent")).Text; * //sw.WriteLine(line.Replace("[xxxMetaTagxxx]", "<meta name=\"" + strMetaName.Trim() + "\" content=\"" + strMetaContent.Trim() + "\" />")); * sw.WriteLine("<meta name=\"" + strMetaName.Trim() + "\" content=\"" + strMetaContent.Trim() + "\" />"); * } * } * } * else if (line.Contains("[xxxProductIDxxx]")) * { * //write the lie to console window * sw.WriteLine(line.Replace("[xxxProductIDxxx]", PId.ToString())); * //sw.WriteLine(PId.ToString()); * } * else * { * sw.WriteLine(line); * } * //Read the next line * line = sr.ReadLine(); * } * //close the file * sr.Close(); * sw.Close(); * chkflag = false; * if (File.Exists(Server.MapPath("~\\" + WrTofileName + "\\" + txtproductname.Text.Trim().Replace(" ", "-") + ".html"))) * chkflag = true; * } * catch (Exception) * { * chkflag = false; * } * }*/ if (chkflag) { SqlTrn.Commit(); ClearControls(); AlertMsg("Record Saved Successfuly"); } else { SqlTrn.Rollback(); string DefaultfileNamelogo = ConfigurationManager.AppSettings["DefaultLogoImg"].ToString(); string DefaultfileNamead = ConfigurationManager.AppSettings["DefaultAddImg"].ToString(); string DefaultfileNamebig = ConfigurationManager.AppSettings["DefaultBigImg"].ToString(); if (DefaultfileNamelogo != fileNamelogo && File.Exists(Server.MapPath(folderPath + fileNamelogo))) { File.Delete(Server.MapPath(folderPath + fileNamelogo)); } if (DefaultfileNamead != fileNamead && File.Exists(Server.MapPath(folderPath + fileNamead))) { File.Delete(Server.MapPath(folderPath + fileNamead)); } if (DefaultfileNamebig != fileNamebig && File.Exists(Server.MapPath(folderPath + fileNamebig))) { File.Delete(Server.MapPath(folderPath + fileNamebig)); } if (File.Exists(Server.MapPath("~\\" + WrTofileName + "\\" + txtproductname.Text.Trim().Replace(" ", "-") + ".html"))) { File.Delete(Server.MapPath("~\\" + WrTofileName + "\\" + txtproductname.Text.Trim().Replace(" ", "-") + ".html")); } } con.Close(); } } catch (Exception) { } }
protected void btnSubmit_Click(object sender, EventArgs e) { bool redirectChk = false; try { string errMsg = ""; errMsg = validateEntry(); if (!String.IsNullOrEmpty(errMsg)) { lblErrMsg.Text = errMsg.Replace("\n", "<br/>"); AlertMsg(errMsg.Replace("\n", "\\n")); //Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "errMsg", "alert('hi')",true); return; } //Variables bool chkflag = false; string chkExNonQry = "0"; string folderPath = ConfigurationManager.AppSettings["ImageFolder"].ToString(); DataTable PriceAdd = gridViewtoDatatable().Copy(); DataTable PriceUpdate = grdUpdateToDataTable().Copy(); //Save Image FIle string DefaultfileNamebig = ConfigurationManager.AppSettings["DefaultBigImg"].ToString(); ValidateImage objImage = new ValidateImage(); string FileNameWtOtExt = ""; folderPath = ConfigurationManager.AppSettings["ImageFolder"].ToString(); string CurrLogoImgFileName = Path.GetFileName(Server.MapPath(imgUpLogo.ImageUrl.Replace("..", "~"))); string CurrAdverImgFileName = Path.GetFileName(Server.MapPath(imgUpAdver.ImageUrl.Replace("..", "~"))); string CurrBigExtraImgFileName = Path.GetFileName(Server.MapPath(imgUpBigImg.ImageUrl.Replace("..", "~"))); if ((fileuplogo.HasFile) && (fileuplogo.PostedFile.ContentLength > 0)) { string DefaultfileNamelogo = ConfigurationManager.AppSettings["DefaultLogoImg"].ToString(); //file Delete Logic Need to check if the file that is to be delete is default image then do not delete if (DefaultfileNamelogo != CurrLogoImgFileName) { File.Delete(Server.MapPath(imgUpLogo.ImageUrl.Replace("..", "~"))); FileNameWtOtExt = Path.GetFileNameWithoutExtension(Server.MapPath(imgUpLogo.ImageUrl.Replace("..", "~"))); string filExt = Path.GetExtension(fileuplogo.PostedFile.FileName); fileuplogo.SaveAs(Server.MapPath(folderPath + FileNameWtOtExt + filExt)); CurrLogoImgFileName = FileNameWtOtExt + filExt; } else { objImage.saveimage(fileuplogo, out CurrLogoImgFileName); } } if ((fileupad.HasFile) && (fileupad.PostedFile.ContentLength > 0)) { string DefaultfileNamead = ConfigurationManager.AppSettings["DefaultAddImg"].ToString(); //file Delete Logic Need to check if the file that is to be delete is default image then do not delete if (DefaultfileNamead != CurrAdverImgFileName) { File.Delete(Server.MapPath(imgUpAdver.ImageUrl.Replace("..", "~"))); FileNameWtOtExt = Path.GetFileNameWithoutExtension(Server.MapPath(imgUpAdver.ImageUrl.Replace("..", "~"))); string filExt = Path.GetExtension(fileupad.PostedFile.FileName); fileupad.SaveAs(Server.MapPath(folderPath + FileNameWtOtExt + filExt)); CurrAdverImgFileName = FileNameWtOtExt + filExt; } else { objImage.saveimage(fileupad, out CurrAdverImgFileName); } } if ((fileuplogobig.HasFile) && (fileuplogobig.PostedFile.ContentLength > 0)) { string DefaultfileNamead = ConfigurationManager.AppSettings["DefaultAddImg"].ToString(); //file Delete Logic Need to check if the file that is to be delete is default image then do not delete if (DefaultfileNamead != CurrBigExtraImgFileName) { File.Delete(Server.MapPath(imgUpBigImg.ImageUrl.Replace("..", "~"))); FileNameWtOtExt = Path.GetFileNameWithoutExtension(Server.MapPath(imgUpBigImg.ImageUrl.Replace("..", "~"))); string filExt = Path.GetExtension(fileuplogobig.PostedFile.FileName); fileuplogobig.SaveAs(Server.MapPath(folderPath + FileNameWtOtExt + filExt)); CurrBigExtraImgFileName = FileNameWtOtExt + filExt; } else { objImage.saveimage(fileuplogobig, out CurrBigExtraImgFileName); } } if (!File.Exists(Server.MapPath(folderPath + CurrLogoImgFileName))) { CurrLogoImgFileName = Path.GetFileName(Server.MapPath(imgUpLogo.ImageUrl.Replace("..", "~"))); } if (!File.Exists(Server.MapPath(folderPath + CurrAdverImgFileName))) { CurrAdverImgFileName = Path.GetFileName(Server.MapPath(imgUpAdver.ImageUrl.Replace("..", "~"))); } if (!File.Exists(Server.MapPath(folderPath + CurrBigExtraImgFileName))) { CurrBigExtraImgFileName = Path.GetFileName(Server.MapPath(imgUpBigImg.ImageUrl.Replace("..", "~"))); } SqlConnection con = objDaAcc.conObj; try { SqlCommand cmd = new SqlCommand("Product_IUD", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ProductId", hdnProductId.Value); cmd.Parameters.AddWithValue("@categoryId", ddlsubcategory.SelectedValue); cmd.Parameters.AddWithValue("@name", txtproductname.Text); cmd.Parameters.AddWithValue("@ShortName", txtShortName.Text); cmd.Parameters.AddWithValue("@description", HttpUtility.HtmlEncode(txtproductdesc.Text)); cmd.Parameters.AddWithValue("@strength", txtstrength.Text); cmd.Parameters.AddWithValue("@expirydate", txtexpiry.Text); cmd.Parameters.AddWithValue("@avgdelivery", txtdelivery.Text); cmd.Parameters.AddWithValue("@price", txtprice.Text); cmd.Parameters.AddWithValue("@logoPrice", txtLogoPrice.Text); cmd.Parameters.AddWithValue("@genername", txtgeneric.Text); cmd.Parameters.AddWithValue("@brandname", txtbrand.Text); cmd.Parameters.AddWithValue("@pageName", (txtproductname.Text.Trim().Replace(" ", "-"))); cmd.Parameters.AddWithValue("@logoimgpath", folderPath + CurrLogoImgFileName); // folderPath = ~/images/products/ and CurrLogoImgFileName = abc.jpg cmd.Parameters.AddWithValue("@adimgpath", folderPath + CurrAdverImgFileName); cmd.Parameters.AddWithValue("@bigimgpath", folderPath + CurrBigExtraImgFileName); cmd.Parameters.AddWithValue("@activeflag", rdActiveDeactive.SelectedValue); cmd.Parameters.AddWithValue("@QuerType", "U"); cmd.Parameters.AddWithValue("@LogInUser", UserInfo.GetUserInfo().userId); cmd.Parameters.AddWithValue("@priceTd", PriceAdd); cmd.Parameters.AddWithValue("@priceTbUpdate", PriceUpdate); if (con.State == ConnectionState.Closed) { con.Open(); SqlTrn = con.BeginTransaction(); cmd.Transaction = SqlTrn; } da = new SqlDataAdapter(cmd); ds = new DataSet(); da.Fill(ds); if ((ds != null) && (ds.Tables != null)) { if (ds.Tables[2] != null) { chkExNonQry = ds.Tables[2].Rows[0][0].ToString(); } if (ds.Tables[0].Rows[0]["PageUrl"] != null) { hdnfilePath.Value = ds.Tables[0].Rows[0]["PageUrl"].ToString(); } } chkflag = true; } catch (Exception) { chkExNonQry = "0"; } //Rename and move to folder /* * if (chkExNonQry == "1") * { * try * { * if (!String.IsNullOrEmpty(hdnfilePath.Value)) * { * //This to rename the file. * FileNameWtOtExt = Path.GetFileNameWithoutExtension(Server.MapPath(hdnfilePath.Value)); * if (FileNameWtOtExt != txtproductname.Text.Trim().Replace(" ", "-")) * { * File.Move(Server.MapPath(hdnfilePath.Value), Server.MapPath(Path.GetDirectoryName(hdnfilePath.Value) + "\\" + txtproductname.Text.Trim().Replace(" ", "-") + ".html")); * } * * //This to move to folder. * string ProdFolderNameStr = "~\\" + ds.Tables[1].Rows[0][0].ToString(); * if (Path.GetDirectoryName(hdnfilePath.Value) != ProdFolderNameStr) * { * File.Move(Server.MapPath(Path.GetDirectoryName(hdnfilePath.Value) + "\\" + txtproductname.Text.Trim().Replace(" ", "-") + ".html"), Server.MapPath(ProdFolderNameStr + "\\" + txtproductname.Text.Trim().Replace(" ", "-") + ".html")); * } * } * chkflag = true; * } * catch (Exception) * { * chkflag = false; * } * * }*/ if (chkflag) { ClearContent(); SqlTrn.Commit(); //AlertMsg("Records Updated Successfuly"); redirectChk = true; } else { SqlTrn.Rollback(); AlertMsg("Error Updating the record."); } con.Close(); } catch (Exception) { SqlTrn.Dispose(); } if (redirectChk) { Response.Redirect("~/Admin/ViewProduct.aspx", true); } }