//Handle the delete button click event public void Delete_Recipes(object sender, DataGridCommandEventArgs e) { if ((e.CommandName == "Delete")) { TableCell iIdNumber2 = e.Item.Cells[0]; TableCell iIRecipename = e.Item.Cells[1]; #region Delete Recipe Image //Delete the recipe image if the recipe has an image. //Instantiate sql params object Blogic FetchData = new Blogic(); try { IDataReader dr = FetchData.GetRecipeImageFileNameForUpdate(int.Parse(iIdNumber2.Text)); dr.Read(); if (dr["RecipeImage"] != DBNull.Value) { System.IO.File.Delete(Server.MapPath(GetRecipeImage.ImagePath + dr["RecipeImage"].ToString())); } dr.Close(); } catch { } FetchData = null; #endregion //Refresh cached data Caching.PurgeCacheItems("MainCourse_RecipeCategory"); Caching.PurgeCacheItems("Ethnic_RecipeCategory"); Caching.PurgeCacheItems("RecipeCategory_SideMenu"); Caching.PurgeCacheItems("Newest_RecipesSideMenu_"); //Instantiate delete recipe object RecipeInfo DelRecipe = new RecipeInfo(); DelRecipe.ID = int.Parse(iIdNumber2.Text); //Perform delete recipe DelRecipe.Delete(); DelRecipe = null; //Redirect to confirm delete page strURLRedirect = "confirmdel.aspx?catname=" + iIRecipename.Text + "&mode=del"; Server.Transfer(strURLRedirect); } }
/// <summary> /// Return the recipe image /// </summary> public static string GetImage(int ID) { //Instantiate sql params object Blogic myBL = new Blogic(); string FileName; FileName = "../RecipeImageUpload/noimageavailable.gif"; IDataReader dr = myBL.GetRecipeImageFileNameForUpdate(ID); dr.Read(); if (dr["RecipeImage"] != DBNull.Value) { FileName = "../RecipeImageUpload/" + (string)dr["RecipeImage"]; } dr.Close(); myBL = null; return(FileName); }
private void Page_Init(Object sender, EventArgs e) { //Instantiate sql params object Blogic myBL = new Blogic(); int ID = (int)Util.Val(Request.QueryString["id"]); string FileName = ""; try { IDataReader dr = myBL.GetRecipeImageFileNameForUpdate(ID); dr.Read(); if (dr["RecipeImage"] != DBNull.Value) { FileName = (string)dr["RecipeImage"]; } dr.Close(); } catch { //Redirect to image not found. //1 = pagenotfound.aspx Util.PageRedirect(1); } string Directory = Server.MapPath("~/RecipeImageUpload/"); string path = Directory + FileName; int roundedDia = 30; using (System.Drawing.Image imgin = System.Drawing.Image.FromFile(path)) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imgin.Width, imgin.Height); Graphics g = Graphics.FromImage(bitmap); g.Clear(Color.White); Brush brush = new System.Drawing.TextureBrush(imgin); FillRoundedRectangle(g, new Rectangle(0, 0, imgin.Width, imgin.Height), roundedDia, brush); // done with drawing dispose graphics object. g.Dispose(); // Stream Image to client. Response.Clear(); Response.ContentType = "image/pjpeg"; bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); Response.End(); //dispose bitmap object. bitmap.Dispose(); //Release allocated memory myBL = null; Util = null; } }
//Handles update recipe public void Update_Recipe(object sender, EventArgs e) { RecipeInfo Update = new RecipeInfo(); Update.ID = (int)Util.Val(Request.QueryString["id"]); Update.RecipeName = Request.Form["Name"]; Update.Author = Request.Form["Author"]; Update.CatID = int.Parse(Request.Form["CategoryID"]); Update.Ingredients = Request.Form["Ingredients"]; Update.Instructions = Request.Form["Instructions"]; Update.Hits = int.Parse(Request.Form["Hits"]); #region Upload New Photo/Update Photo if (RecipeImageFileUpload.HasFile) //Check if there is a file { //Constant variables string Directory = GetRecipeImage.ImagePath; //Directory to store recipe images int maxFileSize = 30000; // Maximum file size limit int FileSize = RecipeImageFileUpload.PostedFile.ContentLength; //Get th file lenght string contentType = RecipeImageFileUpload.PostedFile.ContentType; // Get the file type string FileExist = Directory + RecipeImageFileUpload.PostedFile.FileName; // Get the filename from the directory and compare string FileName = Path.GetFileNameWithoutExtension(RecipeImageFileUpload.PostedFile.FileName); //Get the posted filename string FileExtention = Path.GetExtension(RecipeImageFileUpload.PostedFile.FileName); //Get the posted file extension string FilePath; string FileNameWithExtension; //File type validation if (!contentType.Equals("image/gif") && !contentType.Equals("image/jpeg") && !contentType.Equals("image/jpg") && !contentType.Equals("image/png")) { lbvalenght.Text = "File format is invalid. Only gif, jpg, jpeg or png files are allowed."; lbvalenght.Visible = true; return; } // File size validation else if (FileSize > maxFileSize) { lbvalenght.Text = "File size exceed the maximun allowed 30000 bytes"; lbvalenght.Visible = true; return; } else { //Check wether the image name already exist. //We don't want images stored in the directory if they are not being use. if (File.Exists(Server.MapPath(FileExist))) { //Create a random alpha numeric to make sure the updated image name is unique. Random rand = new Random((int)DateTime.Now.Ticks); int randnum = rand.Next(1, 10); int CharCode = rand.Next(Convert.ToInt32('a'), Convert.ToInt32('z')); char RandomChar = Convert.ToChar(CharCode); //Get directory, the file name and the extension. FilePath = string.Concat(Directory, FileName + randnum + RandomChar, "", FileExtention); //Joined the filename and extension to insert into the database. FileNameWithExtension = FileName + randnum + RandomChar + FileExtention; //Initialize Add recipe object property to get the full image name Update.RecipeImage = FileNameWithExtension; try { //Delete old image File.Delete(Server.MapPath(Directory + FileName + FileExtention)); //Save the recipe image to the specified directory //Make sure the "RecipeImage" folder has write permission to upload image RecipeImageFileUpload.SaveAs(Server.MapPath(FilePath)); } catch (Exception ex) { JSLiteral.Text = "Error: " + ex.Message; return; } } else { //Get directory, the file name and the extension. FilePath = string.Concat(Directory, FileName, "", FileExtention); //Joined the filename and extension to insert into the database. FileNameWithExtension = FileName + FileExtention; //Initialize Add recipe object property to get the full image name Update.RecipeImage = FileNameWithExtension; try { //Save the recipe image to the specified directory //Make sure the "RecipeImage" folder has write permission to upload image RecipeImageFileUpload.SaveAs(Server.MapPath(FilePath)); } catch (Exception ex) { JSLiteral.Text = "Error: " + ex.Message; return; } } } } else { //This section is executed if the input file is empty. //Then it check if an image filename exist in the database. //If it exist, just update it with the same value, else update it with an empty string. IDataReader dr = myBL.GetRecipeImageFileNameForUpdate(Update.ID); dr.Read(); if (dr["RecipeImage"] != DBNull.Value) { Update.RecipeImage = (string)dr["RecipeImage"]; } else { Update.RecipeImage = string.Empty; } dr.Close(); } #endregion //Notify user if error occured. if (Update.Update() != 0) { JSLiteral.Text = Util.JSProcessingErrorAlert; return; } string strURLRedirect; strURLRedirect = "confirmdel.aspx?catname=" + Update.RecipeName + "&mode=update"; //Release allocated memory Update = null; Util = null; Response.Redirect(strURLRedirect); }