コード例 #1
0
        public ActionResult Edit(StoreItem storeItem, IEnumerable <HttpPostedFileBase> files)
        {
            if (ModelState.IsValid)
            {
                // allowed file extensions
                string[] fileExt = { ".png", ".jpg", ".gif", ".jpeg" };

                foreach (var fileBase in files)
                {
                    // save the background image
                    if (fileBase != null && fileBase.ContentLength > 0)
                    {
                        try
                        {
                            // make sure that the file extension is among the allowed
                            if (Array.IndexOf(fileExt, Path.GetExtension(fileBase.FileName.ToLower())) < 0)
                            {
                                throw new Exception(String.Format("File extension not allowed. Only these files are allowed:<br><br>{0}", string.Join(", ", fileExt)));
                            }

                            var fileName = Path.GetFileName(fileBase.FileName);

                            // fix bug that iPad/iPhone names all the files "image.jpg"
                            if (fileName == "image.jpg")
                            {
                                fileName = String.Format("{0}{1}", CustomHelpers.GeneratePassword(6), Path.GetExtension(fileBase.FileName.ToLower()));
                            }

                            var path = Server.MapPath("~/Images/Store/");

                            // instantiate object
                            var image = new CircuitBentCMS.Models.StoreItemImage();
                            image.ImageUrl    = fileName;
                            image.StoreItemId = storeItem.StoreItemId;

                            context.StoreItemImages.Add(image);

                            fileBase.SaveAs(Path.Combine(path, fileName));

                            ImageBuilder.Current.Build(Path.Combine(path, fileName), Path.Combine(path, "thumb_" + fileName), new ResizeSettings("maxwidth=240&maxheight=240&crop=auto"));
                        }
                        catch (Exception e)
                        {
                            TempData["ErrorMessage"] = e.Message;
                        }
                    }
                }

                context.Entry(storeItem).State = EntityState.Modified;
                context.SaveChanges();

                // display a friendly success message
                TempData["StatusMessage"] = "The changes were saved successfully!";

                return(RedirectToAction("Edit", new { id = storeItem.StoreItemId }));
            }
            return(View(storeItem));
        }
コード例 #2
0
        public ActionResult ImageUpload(IEnumerable <HttpPostedFileBase> files)
        {
            TempData["ErrorMessage"] = "";
            // allowed file extensions
            string[] fileExt = { ".png", ".jpg", ".gif", ".jpeg" };

            foreach (var fileBase in files)
            {
                // make sure the image exists
                if (fileBase != null && fileBase.ContentLength > 0)
                {
                    try
                    {
                        // make sure that the file extension is among the allowed
                        if (Array.IndexOf(fileExt, Path.GetExtension(fileBase.FileName.ToLower())) < 0)
                        {
                            throw new Exception("Image was in an unsupported format. Only images (JPEG, GIF, PNG) allowed.");
                        }

                        var fileName = Path.GetFileName(fileBase.FileName);

                        // fix bug that iPad/iPhone names all the files "image.jpg"
                        var userAgent = HttpContext.Request.UserAgent.ToLower();
                        if (userAgent.Contains("iphone;") || userAgent.Contains("ipad;"))
                        {
                            fileName = String.Format("{0}{1}", CustomHelpers.GeneratePassword(6), Path.GetExtension(fileBase.FileName.ToLower()));
                        }

                        var path = Server.MapPath("~/Images/ImageGallery/");

                        // instantiate object
                        var image = new CircuitBentCMS.Models.Image();
                        image.ImageUrl        = fileName;
                        image.Title           = "";
                        image.Photographer    = "";
                        image.PhotographerUrl = "";

                        // find out how many images there are and get the image order
                        image.Order = context.Images.ToList().Count + 1;

                        context.Images.Add(image);
                        context.SaveChanges();

                        fileBase.SaveAs(Path.Combine(path, fileName));

                        // if the image is larger than 0,5 mb, shrink it
                        if (fileBase.ContentLength > 500000)
                        {
                            ImageBuilder.Current.Build(Path.Combine(path, fileName), Path.Combine(path, fileName), new ResizeSettings("maxwidth=1600&maxheight=1600&crop=auto"));
                        }

                        // get the size of the thumbnails from ImageGallerySettings table
                        var imageGallerySettings = context.ImageGallerySettings.FirstOrDefault();
                        ImageBuilder.Current.Build(Path.Combine(path, fileName), Path.Combine(path, "thumb_" + fileName), new ResizeSettings(String.Format("width={0}&height={1}&crop=auto", imageGallerySettings.ThumbnailWidth, imageGallerySettings.ThumbnailHeight)));
                    }
                    catch (Exception e)
                    {
                        TempData["ErrorMessage"] = e.Message;
                    }
                }
            }

            TempData["StatusMessage"]   = "Images uploaded successfully!<br><br>If you want to supply additional information (like title, photographer etc) about the image, just edit it.";
            TempData["MessageDuration"] = 5000;

            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public ActionResult FileUpload(IEnumerable <HttpPostedFileBase> files)
        {
            // allowed file extensions
            string[] fileExt = { ".png", ".jpg",  ".gif", ".jpeg", ".zip", ".rar", ".flac",   ".pdf",
                                 ".doc", ".docx", ".psd", ".eps",  ".ai",  ".wav", ".tar.gz", ".odt",
                                 ".txt", ".rtf",  ".mp3", ".ogg",  ".mov", ".mkv", ".mpeg",   ".mpeg2", ".html" };
            string[] imageExt = { ".png", ".jpg", ".gif", ".jpeg" };


            foreach (var fileBase in files)
            {
                // save the background image
                if (fileBase != null && fileBase.ContentLength > 0)
                {
                    try
                    {
                        // make sure that the file extension is among the allowed
                        if (Array.IndexOf(fileExt, Path.GetExtension(fileBase.FileName.ToLower())) < 0)
                        {
                            throw new Exception(String.Format("File extension not allowed. Only these files are allowed:<br><br>{0}", string.Join(", ", fileExt)));
                        }

                        var fileName = Path.GetFileName(fileBase.FileName);

                        // fix bug that iPad/iPhone names all the files "image.jpg"
                        var userAgent = HttpContext.Request.UserAgent.ToLower();
                        if (userAgent.Contains("iphone;") || userAgent.Contains("ipad;"))
                        {
                            fileName = String.Format("{0}{1}", CustomHelpers.GeneratePassword(6), Path.GetExtension(fileBase.FileName.ToLower()));
                        }

                        var path = Server.MapPath("~/UserUploads");

                        // instantiate object
                        var file = new CircuitBentCMS.Models.File();
                        file.FileName      = fileName;
                        file.FileExtension = Path.GetExtension(fileBase.FileName);
                        file.LastUpdated   = DateTime.Now;

                        context.Files.Add(file);
                        context.SaveChanges();

                        fileBase.SaveAs(Path.Combine(path, fileName));

                        // check if it's an image, in that case save a thumbnail
                        if (Array.IndexOf(imageExt, Path.GetExtension(fileBase.FileName.ToLower())) > -1)
                        {
                            // if the image is larger than 0,5 mb, shrink it
                            if (fileBase.ContentLength > 500000)
                            {
                                ImageBuilder.Current.Build(Path.Combine(path, fileName), Path.Combine(path, fileName), new ResizeSettings("maxwidth=1600&maxheight=1600&crop=auto"));
                            }
                            // creat thumbnail
                            ImageBuilder.Current.Build(Path.Combine(path, fileName), Path.Combine(path, "thumb_" + fileName), new ResizeSettings("maxwidth=120&maxheight=120&crop=auto"));
                        }
                    }
                    catch (Exception e)
                    {
                        TempData["ErrorMessage"] = e.Message;
                    }
                }
            }

            // if the upload happened from the popup window, redirect to the view withouth an alternative layout
            if (String.IsNullOrEmpty(Request.Form["UsePopupLayout"]))
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("InsertFile"));
            }
        }
コード例 #4
0
        public ActionResult ImageUpload(IEnumerable <HttpPostedFileBase> files)
        {
            // allowed file extensions
            string[] fileExt        = { ".png", ".jpg", ".gif", ".jpeg" };
            var      imageSliderId  = Convert.ToInt32(Request.Form["ImageSliderId"]);
            var      addSliderTitle = Request.Form["AddSliderTitle"];

            // check if the user have selected an existing slider, or wants to create a new
            try
            {
                if (!String.IsNullOrEmpty(addSliderTitle))
                {
                    // make sure that there isn't already a slider with the same name
                    var checkIfSliderExists = context.ImageSliders.SingleOrDefault(a => a.Title == addSliderTitle);
                    if (checkIfSliderExists != null)
                    {
                        throw new Exception("There already exists an image slider with that name. Please select another name.");
                    }

                    // add new image slider to db
                    ImageSlider imageSlider = new ImageSlider
                    {
                        Title = addSliderTitle
                    };
                    context.ImageSliders.Add(imageSlider);
                    context.SaveChanges();

                    // set the image slider id to the newly created
                    imageSliderId = imageSlider.ImageSliderId;
                }
            }
            catch (Exception e)
            {
                TempData["ErrorMessage"] = e.Message;

                return(RedirectToAction("Index"));
            }

            foreach (var fileBase in files)
            {
                // check if the image is valid
                if (fileBase != null && fileBase.ContentLength > 0)
                {
                    try
                    {
                        // make sure that the file extension is among the allowed
                        if (Array.IndexOf(fileExt, Path.GetExtension(fileBase.FileName.ToLower())) < 0)
                        {
                            throw new Exception("Image was in an unsupported format. Only images (JPEG, GIF, PNG) allowed.");
                        }

                        // make sure that the image slider has a correct value
                        if (imageSliderId == 0)
                        {
                            throw new Exception("You have to either select an existing image slider, or create a new one.");
                        }

                        var fileName = Path.GetFileName(fileBase.FileName);

                        // fix bug that iPad/iPhone names all the files "image.jpg"
                        var userAgent = HttpContext.Request.UserAgent.ToLower();
                        if (userAgent.Contains("iphone;") || userAgent.Contains("ipad;"))
                        {
                            fileName = String.Format("{0}{1}", CustomHelpers.GeneratePassword(6), Path.GetExtension(fileBase.FileName.ToLower()));
                        }

                        var path = Server.MapPath("~/Images/ImageSlider/");

                        // instantiate object
                        var image = new CircuitBentCMS.Models.ImageSliderImage();
                        image.ImageUrl      = fileName;
                        image.LinkUrl       = "";
                        image.Title         = "";
                        image.ImageSliderId = imageSliderId;

                        // find out how many images there are and get the image order
                        image.Order = context.ImageSliderImages
                                      .Where(a => a.ImageSliderId == imageSliderId)
                                      .ToList().Count + 1;

                        context.ImageSliderImages.Add(image);
                        context.SaveChanges();

                        fileBase.SaveAs(Path.Combine(path, fileName));

                        // if the image is larger than 0,5 mb, shrink it
                        if (fileBase.ContentLength > 500000)
                        {
                            ImageBuilder.Current.Build(Path.Combine(path, fileName), Path.Combine(path, fileName), new ResizeSettings("maxwidth=1600&maxheight=1600&crop=auto"));
                        }

                        ImageBuilder.Current.Build(Path.Combine(path, fileName), Path.Combine(path, "thumb_" + fileName), new ResizeSettings("maxwidth=180&maxheight=80&crop=auto"));
                    }
                    catch (Exception e)
                    {
                        TempData["ErrorMessage"] = e.Message;
                    }
                }
            }

            return(RedirectToAction("Index"));
        }