示例#1
0
        public ActionResult Add(AddAccessorBm bind, IEnumerable <HttpPostedFileBase> images)
        {
            if (this.ModelState.IsValid)
            {
                foreach (var img in images)
                {
                    if (img != null)
                    {
                        if (img.ContentLength > (5 * 1024 * 1024))
                        {
                            ModelState.AddModelError("CustomError", "File size must be less than 5 MB");
                            return(View());
                        }
                        if (img.ContentType != "image/jpeg")
                        {
                            ModelState.AddModelError("CustomError", "File type must be \"jpeg\"");
                            return(View());
                        }
                    }
                }
                this.service.AddNewAccessor(bind, images);

                return(RedirectToAction("AdminAccessoriesList"));
            }

            return(this.View());
        }
        public void AddNewAccessor(AddAccessorBm bind, IEnumerable <HttpPostedFileBase> images)
        {
            Accessor accessor = Mapper.Map <AddAccessorBm, Accessor>(bind);
            int      i        = 1;

            foreach (var img in images)
            {
                if (img != null)
                {
                    switch (i)
                    {
                    case 1:
                        accessor.Image1 = new byte[img.ContentLength];
                        img.InputStream.Read(accessor.Image1, 0, img.ContentLength);
                        break;

                    case 2:
                        accessor.Image2 = new byte[img.ContentLength];
                        img.InputStream.Read(accessor.Image2, 0, img.ContentLength);
                        break;

                    case 3:
                        accessor.Image3 = new byte[img.ContentLength];
                        img.InputStream.Read(accessor.Image3, 0, img.ContentLength);
                        break;
                    }
                    i++;
                }
                this.Context.Accessoaries.Add(accessor);
                this.Context.SaveChanges();
            }
        }