Example #1
0
        public ActionResult Create(CreateSelectViewModel viewModel)
        {
            var validImageTypes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };
            var valids = viewModel.Options.Select(x => x.ValueFile != null &&
                                                  validImageTypes.Contains(x.ValueFile.ContentType)).Count();

            if ((SelectType)Enum.Parse(typeof(SelectType), viewModel.Type, true) == SelectType.Image && valids != viewModel.Options.Count)
            {
                TempData["IsSuccess"] = false;
                TempData["Message"]   = string.Format(@"Please choose either a GIF, JPG or PNG image");
            }
            else if ((SelectType)Enum.Parse(typeof(SelectType), viewModel.Type, true) == SelectType.Image && valids == viewModel.Options.Count)
            {
                foreach (var option in viewModel.Options)
                {
                    if (option.ValueFile != null)
                    {
                        using (System.Drawing.Image image = System.Drawing.Image.FromStream(option.ValueFile.InputStream, true, true))
                        {
                            if (!Directory.Exists(Server.MapPath(PathConstant.SelectPath)))
                            {
                                Directory.CreateDirectory(Server.MapPath(PathConstant.SelectPath));
                            }
                            var imagePath = Path.Combine(Server.MapPath(PathConstant.SelectPath), option.ValueFile.FileName);
                            option.ValueFile.SaveAs(imagePath);
                            option.Value = option.ValueFile.FileName;
                        }
                    }
                }
            }
            var request  = viewModel.MapTo <CreateSelectRequest>();
            var response = _selectService.Create(request);

            if (response.IsSuccess)
            {
                return(RedirectToAction("Index"));
            }

            return(View(viewModel));
        }