Пример #1
0
        public IHttpActionResult AddImage(HttpRequestMessage request, Guid id)
        {
            try
            {
                var uploadPath = HttpContext.Current.Server.MapPath("~/Content/cows");

                var multipartFormDataStreamProvider = new UploadMultipartFormProvider(uploadPath);

                // Read the MIME multipart asynchronously
                Request.Content.ReadAsMultipartAsync(multipartFormDataStreamProvider);

                var localFileName = multipartFormDataStreamProvider
                    .FileData.Select(multiPartData => multiPartData.LocalFileName).FirstOrDefault();

                // Create response
                if (localFileName != null)
                {
                    var fileUploadResult = new FileUploadResult
                    {
                        LocalFilePath = localFileName,
                        FileName = Path.GetFileName(localFileName),
                        FileLength = new FileInfo(localFileName).Length
                    };

                    var cow = _businessLogic.GetCowById(id);
                    cow.Image = fileUploadResult.FileName;

                    _businessLogic.Update(cow);
                }
                return Ok();
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        public IHttpActionResult AddImage(HttpRequestMessage request, string email)
        {
            try
            {
                var uploadPath = HttpContext.Current.Server.MapPath("~/Content/users");

                var multipartFormDataStreamProvider = new UploadMultipartFormProvider(uploadPath);

                // Read the MIME multipart asynchronously
                Request.Content.ReadAsMultipartAsync(multipartFormDataStreamProvider);

                string _localFileName = multipartFormDataStreamProvider
                    .FileData.Select(multiPartData => multiPartData.LocalFileName).FirstOrDefault();

                // Create response
                FileUploadResult fileUploadResult = new FileUploadResult
                {
                    LocalFilePath = _localFileName,

                    FileName = Path.GetFileName(_localFileName),

                    FileLength = new FileInfo(_localFileName).Length
                };

                var user = _userBusinessLogic.GetUserByEmail(email);
                user.Image = $"Content/users/{fileUploadResult.FileName}";

                _userBusinessLogic.UpdateUser(user.Id, user);

                return Ok();
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }