public ActionResult AddOrEdit(Product product, HttpPostedFileBase uploadedFile) { var allowedExtensions = new[] { ".GIF", ".PNG", ".JPG", ".JPEG" }; if (uploadedFile != null) { var ext = Path.GetExtension(uploadedFile.FileName); if (allowedExtensions.Contains(ext.ToUpper())) //check what type of extension { string myfile = "ProductImage" + DateTime.Now.ToString("ddMMyyhhmm") + ext; var path = ConfigurationManager.AppSettings["ProductImage"]; var finalpath = Path.Combine(Server.MapPath(path), myfile); if (product.ProductId > 0) { var imageName = product.ProductImage; var existingpath = ConfigurationManager.AppSettings["ProductImage"]; if (System.IO.File.Exists(Server.MapPath(existingpath + imageName))) { System.IO.File.Delete(Server.MapPath(existingpath + imageName)); } } product.ProductImage = myfile; uploadedFile.SaveAs(finalpath); } else { Message message = new Message(); message.ReturnMessage = "Choose only Image File!"; message.MessageType = MessageTypes.Information; } } else { Message message = new Message(); message.ReturnMessage = "Select an Image!"; message.MessageType = MessageTypes.Information; } var data = _iProductManager.AddOrEdit(product); return(Json(new { messageType = data.MessageType, message = data.ReturnMessage, html = GlobalClass.RenderRazorViewToString(this, "ViewAll", _iProductManager.GetAllProduct()) }, JsonRequestBehavior.AllowGet)); }