Пример #1
0
        public HttpResponseMessage Upload()
        {
            var httpPostedFile = HttpContext.Current.Request.Files["imageFile"];
            var productID      = HttpContext.Current.Request.Form[0];

            BinaryReader reader = new BinaryReader(httpPostedFile.InputStream);

            var image = reader.ReadBytes(httpPostedFile.ContentLength);

            var avatarOfProduct = new AvatarOfProduct();

            avatarOfProduct.ProductID = int.Parse(productID);
            avatarOfProduct.Avatar    = image;

            var product = this._unitOfWork.Products.Get(avatarOfProduct.ProductID);

            if (product == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (!IsAuthorized(product.SellerID))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            this._unitOfWork.AvatarOfProducts.Add(avatarOfProduct);
            this._unitOfWork.Complete();

            return(GetResponseMessage(avatarOfProduct));
        }
Пример #2
0
        private HttpResponseMessage GetResponseMessage(AvatarOfProduct avatarOfProduct)
        {
            MemoryStream ms = new MemoryStream(avatarOfProduct.Avatar);

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new StreamContent(ms);

            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");

            return(response);
        }