Example #1
0
        public ActionResult Edit([Bind(Include = "ProductID,ProductName,Price,UnitsSold,TypeID,MakeID,Model,ProductStatusID,Description,ProductImage,IsFeatured")] Product product, HttpPostedFileBase productImage)
        {
            if (ModelState.IsValid)
            {
                #region File Edit
                string file = "noImage.png";

                if (productImage != null)
                {
                    // The user has uploaded a file to include for that specific book.
                    file = productImage.FileName;
                    // Check that the uploaded file extension matches accepted extensions
                    string   ext      = file.Substring(file.LastIndexOf('.'));
                    string[] goodExts = { ".jpg", ".jpeg", ".png", ".gif" };

                    // Check that the file extension is in our list of acceptable extensions AND check that the file size is 4MB MAX
                    if (goodExts.Contains(ext.ToLower()) && productImage.ContentLength <= 5242880)
                    {
                        // Create a new file name (using a GUID)
                        file = Guid.NewGuid() + ext;

                        #region Resize Image
                        // This informs the program to save the image to this location in our file structure.
                        string savePath = Server.MapPath("~/Content/images/");

                        Image convertedImage = Image.FromStream(productImage.InputStream);

                        int maxImageSize = 400;

                        int maxThumbSize = 100;

                        ImageServices.ResizeImage(savePath, file, convertedImage, maxImageSize, maxThumbSize);
                        #endregion

                        // The code below will delete the old image from the file structure.
                        if (product.ProductImage != null && product.ProductImage != "noImage.png")
                        {
                            string path = Server.MapPath("~/Content/images/");
                            ImageServices.Delete(path, product.ProductImage);
                        }
                    }
                }

                // No matter what, update the photoUrl with the value of the file variable
                product.ProductImage = file;
                #endregion
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.MakeID          = new SelectList(db.ProductMakes, "MakeID", "MakeName", product.MakeID);
            ViewBag.ProductStatusID = new SelectList(db.ProductStatuses, "ProductStatusID", "ProductStatusName", product.ProductStatusID);
            ViewBag.TypeID          = new SelectList(db.ProductTypes, "TypeID", "TypeName", product.TypeID);
            return(View(product));
        }
        public ActionResult Create([Bind(Include = "ProductID,ProductName,ProductDescription,Price,UnitsInStock,ProductImage,ProductStatusID")] Product product, HttpPostedFileBase imageUpload)
        {
            if (ModelState.IsValid)
            {
                #region imageUpload using Image Service

                //string for default file name
                string fileName = "NoPicAvailable.png";
                if (imageUpload != null)
                {
                    //process image
                    fileName = imageUpload.FileName;
                    //get ext
                    string ext = fileName.Substring(fileName.LastIndexOf("."));
                    //list allowed exts
                    string[] goodExts = { ".jpg", ".jpeg", ".png", ".gif" };
                    //check exts
                    if (goodExts.Contains(ext.ToLower()))
                    {
                        //assign GUID for filename
                        fileName = Guid.NewGuid() + ext;

                        //************************************Image Service Prep to resize image params

                        string savePath = Server.MapPath("~/Content/images/");
                        //convert the uploaded file from HttpPostedFilebase to Image
                        Image convertedImage = Image.FromStream(imageUpload.InputStream);

                        //maxsize
                        int maxSize = 800;
                        //maxthumb
                        int maxThumb = 250;

                        //call ResizeImage() from the class
                        ImageServices.ResizeImage(savePath, fileName, convertedImage, maxSize, maxThumb);
                    }
                }
                //regardless of wether was an upload, update the productimage property of the new record
                product.ProductImage = fileName;
                #endregion

                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ProductStatusID = new SelectList(db.ProductStatuses, "ProductStatusID", "StatusName", product.ProductStatusID);
            return(View(product));
        }
Example #3
0
        public ActionResult Create([Bind(Include = "PetID,Name,OwnerID,PetPhoto,SpecialNotes,IsActive,DateAdded,TypeOfAnimal")] Pet pet, HttpPostedFileBase petImage)
        {
            if (ModelState.IsValid)
            {
                //db.Pets.Add(pet);
                //db.SaveChanges();
                //return RedirectToAction("Index");
                #region Image Upload

                string imageName = "no-image-found.jpg";
                if (petImage != null)
                {
                    string imgExt = Path.GetExtension(petImage.FileName).ToLower();

                    string[] allowedExtensions = { ".jpg", ".png", ".gif", ".jpeg" };

                    if (allowedExtensions.Contains(imgExt))
                    {
                        imageName = Guid.NewGuid().ToString() + imgExt;

                        string savePath = Server.MapPath("~/Content/Images/Pets/");

                        Image convertedImage = Image.FromStream(petImage.InputStream);

                        int maxImageSize = 600;

                        int maxThumbnailSize = 100;

                        ImageServices.ResizeImage(savePath, imageName, convertedImage, maxImageSize, maxThumbnailSize);
                    }
                    else
                    {
                        log.Info(User.Identity.Name + " tried to upload a pet photo " + imgExt + "when registering a pet.");
                    }
                    #endregion
                }

                pet.OwnerID   = User.Identity.GetUserId();
                pet.IsActive  = true;
                pet.DateAdded = DateTime.Now;
                pet.PetPhoto  = imageName;
                uow.PetRepository.Add(pet);
                uow.Save();
                return(RedirectToAction("Index"));
            }

            return(View(pet));
        }
Example #4
0
        public ActionResult Create([Bind(Include = "ProductID,ProductName,Price,UnitsSold,TypeID,MakeID,Model,ProductStatusID,Description,ProductImage,IsFeatured")] Product product, HttpPostedFileBase productImage)
        {
            if (ModelState.IsValid)
            {
                #region File Upload
                string file = "noImage.png";

                if (productImage != null)
                {
                    file = productImage.FileName;

                    string   ext      = file.Substring(file.LastIndexOf('.'));
                    string[] goodExts = { ".jpg", ".jpeg", ".png", ".gif" };

                    if (goodExts.Contains(ext.ToLower()) && productImage.ContentLength <= 5242880)
                    {
                        file = Guid.NewGuid() + ext;

                        #region Resize
                        string savePath = Server.MapPath("~/Content/images/");

                        Image resized = Image.FromStream(productImage.InputStream);

                        int maxImageSize = 400;
                        int maxThumbSize = 100;

                        ImageServices.ResizeImage(savePath, file, resized, maxImageSize, maxThumbSize);

                        #endregion
                    }
                }
                product.ProductImage = file;
                #endregion
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.MakeID          = new SelectList(db.ProductMakes, "MakeID", "MakeName", product.MakeID);
            ViewBag.ProductStatusID = new SelectList(db.ProductStatuses, "ProductStatusID", "ProductStatusName", product.ProductStatusID);
            ViewBag.TypeID          = new SelectList(db.ProductTypes, "TypeID", "TypeName", product.TypeID);
            return(View(product));
        }
Example #5
0
        public ActionResult Create([Bind(Include = "ProductId,ProductName,ProductDescription,Price,UnitsInStock,ProductImage,ProductStatusId")] Product product, HttpPostedFileBase gmImageUpload)
        {
            if (ModelState.IsValid)
            {
                #region FILE UPLOAD (CREATE) WITH IMAGE SERVICE
                string imageName = "NoImage.jpg";

                if (gmImageUpload != null)
                {
                    //find the extension
                    imageName = gmImageUpload.FileName;
                    string imgExtention = imageName.Substring(imageName.LastIndexOf("."));
                    //string imgExtention = System.IO.Path.GetExtension(bkImageUpload.FileName); Another option to get extension


                    string[] goodExtensions = { ".jpg", ".jpeg", ".gif", ".png" };

                    if (goodExtensions.Contains(imgExtention.ToLower()))
                    {
                        imageName = Guid.NewGuid() + imgExtention; //created unique filename

                        //rather than save this file to the server directly, we'll use the ImageService
                        //to do it - makes 2 copies, main & thumbnail.

                        //let's prep all the parameters that will be needed.
                        string imgPath = Server.MapPath("~/Content/Images/_BoxCovers/");    //folder path

                        Image convertedImage = Image.FromStream(gmImageUpload.InputStream); //get file as image

                        //choose max img size in pixels
                        int maxImageSize = 400;
                        // choose max img size for thumbnail in pixels
                        int maxThumbSize = 100;

                        //call image service to rezie and save images

                        ImageServices.ResizeImage(imgPath, imageName, convertedImage, maxImageSize, maxThumbSize);
                    }
                    else
                    {
                        //handle invalid file type somehow....
                        imageName = "NoImage.jpg";
                    }
                }

                //regardless of whether a file was uploaded, set the image name on the db record (hijack record)
                product.ProductImage = imageName;

                #endregion

                //db.Products.Add(product);
                //db.SaveChanges();

                uow.ProductRepository.Add(product);
                uow.Save();
                return(RedirectToAction("Index"));
            }

            ViewBag.ProductStatusId = new SelectList(uow.ProductStatusRepository.Get(), "ProductStatusId", "StatusName", product.ProductStatusId);
            return(View(product));
        }