Пример #1
0
        public ActionResult AddImages()
        {
            ImageViewModel imagesView = new ImageViewModel();
            string currentUserId = HttpContext.User.Identity.GetUserId();

            var albums = ImagesContext.Albums.Where(x => x.ApplicationUserID == currentUserId);

            if (albums.Count() == 0)
            {
                initializeDefaultAlbum();
                albums = ImagesContext.Albums.Where(x => x.ApplicationUserID == currentUserId);
            }

            var categories = ImagesContext.Categories.ToList();

            SelectList selectlist = new SelectList(albums.AsEnumerable(), "AlbumID", "Name");
            imagesView.AlbumsDropDown = selectlist;
            imagesView.selectedAlbum = 1;

            SelectList selectCategories = new SelectList(categories.AsEnumerable(), "CategoryID", "Name");
            imagesView.CategoriesDropDown = selectCategories;
            imagesView.selectedCategory = 1;

            return View(imagesView);
        }
Пример #2
0
        public ActionResult AddImages(ImageViewModel model)
        {
            var validImageTypes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };

            if (model.FilesUpload == null || model.FilesUpload.Count() == 0)
            {
                ModelState.AddModelError("Upload image(s)", "This field is required");
            }

            if (ModelState.IsValid)
            {
                bool error = false;
                LinkedList<string> imageTitles = new LinkedList<string>();

                if (!String.IsNullOrEmpty(model.Title))
                    imageTitles = new LinkedList<string>(model.Title.Split(',').ToList());

                //System.Diagnostics.Debug.WriteLine("Image URL " + "asd");

                int titleIndex = 0;

                foreach (var imageToUpload in model.FilesUpload)
                {
                    if (imageToUpload == null)
                        break;

                    if (!validImageTypes.Contains(imageToUpload.ContentType))
                    {
                        ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image.");
                        error = true;
                        break;
                    }

                    if (imageToUpload != null && imageToUpload.ContentLength > 0)
                    {
                        Image image = new Image();

                        if (titleIndex < imageTitles.Count)
                            image.Title = imageTitles.ElementAt(titleIndex);
                        else
                            image.Title = imageToUpload.FileName;

                        ++titleIndex;

                        string uploadDir = AppDomain.CurrentDomain.BaseDirectory + "UploadedImages\\";
                        //var imagePath = Path.Combine(Server.MapPath(uploadDir), imageToUpload.FileName);
                        //var imageUrl = Path.Combine(uploadDir, imageToUpload.FileName);
                        string filename = Path.GetFileName(imageToUpload.FileName);
                        imageToUpload.SaveAs(Path.Combine(uploadDir, filename));
                        image.ImageUrl = Path.Combine(uploadDir, filename);
                        image.AlbumID = model.selectedAlbum;
                        image.CategoryID = model.selectedCategory;

                        ImagesContext.Images.Add(image);
                        ImagesContext.SaveChanges();
                    }
                    else
                    {
                        error = true;
                        break;
                    }
                }

                return RedirectToAction("AddImages", "Images");
            }

            return RedirectToAction("AddImages", "Images");
        }