コード例 #1
0
        public IHttpActionResult AddItem()
        {
            if (!HttpContext.Current.Request.Files.AllKeys.Any())
            {
                throw new ValidationException(ErrorCodes.EmptyItemImage);
            }
            var httpPostedFile  = HttpContext.Current.Request.Files[0];
            var httpPostedFile2 = HttpContext.Current.Request.Files[1];
            var httpPostedFile3 = HttpContext.Current.Request.Files[2];

            var itemModel = new JavaScriptSerializer().Deserialize <ItemModel>(HttpContext.Current.Request.Form.Get(0));

            if (httpPostedFile == null || httpPostedFile2 == null || httpPostedFile3 == null)
            {
                throw new ValidationException(ErrorCodes.EmptyItemImage);
            }

            if ((httpPostedFile.ContentLength > 2 * 1024 * 1000) || (httpPostedFile2.ContentLength > 2 * 1024 * 1000) || (httpPostedFile3.ContentLength > 2 * 1024 * 1000))
            {
                throw new ValidationException(ErrorCodes.ImageExceedSize);
            }


            if (Path.GetExtension(httpPostedFile.FileName).ToLower() != ".jpg" &&
                Path.GetExtension(httpPostedFile.FileName).ToLower() != ".png" &&
                Path.GetExtension(httpPostedFile.FileName).ToLower() != ".jpeg")
            {
                throw new ValidationException(ErrorCodes.InvalidImageType);
            }

            if (Path.GetExtension(httpPostedFile2.FileName).ToLower() != ".jpg" &&
                Path.GetExtension(httpPostedFile2.FileName).ToLower() != ".png" &&
                Path.GetExtension(httpPostedFile2.FileName).ToLower() != ".jpeg")
            {
                throw new ValidationException(ErrorCodes.InvalidImageType);
            }


            if (Path.GetExtension(httpPostedFile3.FileName).ToLower() != ".jpg" &&
                Path.GetExtension(httpPostedFile3.FileName).ToLower() != ".png" &&
                Path.GetExtension(httpPostedFile3.FileName).ToLower() != ".jpeg")
            {
                throw new ValidationException(ErrorCodes.InvalidImageType);
            }

            var itemDto = Mapper.Map <ItemDTO>(itemModel);

            itemDto.Image = new MemoryStream();
            httpPostedFile.InputStream.CopyTo(itemDto.Image);


            itemDto.Image2 = new MemoryStream();
            httpPostedFile2.InputStream.CopyTo(itemDto.Image2);

            itemDto.Image3 = new MemoryStream();
            httpPostedFile3.InputStream.CopyTo(itemDto.Image3);

            _itemFacade.AddItem(itemDto, HostingEnvironment.MapPath("~/Images/"));
            return(Ok());
        }