コード例 #1
0
        public IHttpActionResult UpdateItem()
        {
            var itemModel =
                new JavaScriptSerializer().Deserialize <ItemModel>(HttpContext.Current.Request.Form.Get(0));
            var itemDto = Mapper.Map <ItemDTO>(itemModel);

            if (itemModel.IsImageChange)
            {
                if (!HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    throw new ValidationException(ErrorCodes.EmptyItemImage);
                }
                var httpPostedFile = HttpContext.Current.Request.Files["file"];


                if (httpPostedFile == null)
                {
                    throw new ValidationException(ErrorCodes.EmptyCategoryImage);
                }

                if (httpPostedFile.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);
                }

                //restaurantDto.Image = (MemoryStream) restaurant.Image.InputStream;
                itemDto.Image = new MemoryStream();
                httpPostedFile.InputStream.CopyTo(itemDto.Image);
            }
            if (itemModel.IsImage2Change)
            {
                if (!HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    throw new ValidationException(ErrorCodes.EmptyItemImage);
                }
                var httpPostedFile2 = HttpContext.Current.Request.Files["file2"];


                if (httpPostedFile2 == null)
                {
                    throw new ValidationException(ErrorCodes.EmptyCategoryImage);
                }

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


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

                //restaurantDto.Image = (MemoryStream) restaurant.Image.InputStream;
                itemDto.Image2 = new MemoryStream();
                httpPostedFile2.InputStream.CopyTo(itemDto.Image2);
            }
            if (itemModel.IsImage3Change)
            {
                if (!HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    throw new ValidationException(ErrorCodes.EmptyItemImage);
                }
                var httpPostedFile3 = HttpContext.Current.Request.Files["file3"];


                if (httpPostedFile3 == null)
                {
                    throw new ValidationException(ErrorCodes.EmptyCategoryImage);
                }

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


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

                //restaurantDto.Image = (MemoryStream) restaurant.Image.InputStream;
                itemDto.Image3 = new MemoryStream();
                httpPostedFile3.InputStream.CopyTo(itemDto.Image3);
            }
            _itemFacade.UpdateItem(itemDto, HostingEnvironment.MapPath("~/Images/"));
            return(Ok());
        }