Пример #1
0
        public async Task <HttpResponseMessage> PostImage()
        {
            HttpResponseMessage response;

            if (!Request.Content.IsMimeMultipartContent())
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, "Unsupported request type. Must have a file.");
            }

            try {
                MultipartStreamProvider provider = new BlobStorageMultipartStreamProvider();
                await Request.Content.ReadAsMultipartAsync(provider);

                response = Request.CreateResponse(HttpStatusCode.OK, "File uploaded successfully.");
            } catch (Exception err) {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, err.Message);
            }

            return(response);
        }
Пример #2
0
        public async Task <IHttpActionResult> UploadFile(string username)
        {
            var isAllowed = ClaimsAuthorization.CheckAccess("Upload", "User", username);

            if (!isAllowed)
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            var userDto = await _userService.GetSingleAsync(username);

            if (userDto == null)
            {
                return(NotFound());
            }
            MultipartStreamProvider provider = new BlobStorageMultipartStreamProvider(userDto);
            await Request.Content.ReadAsMultipartAsync(provider);

            var avatar      = provider.Contents.SingleOrDefault(p => p.Headers.ContentDisposition.Name.Contains("avatar"));
            var headerImage =
                provider.Contents.SingleOrDefault(p => p.Headers.ContentDisposition.Name.Contains("headerImage"));

            if (avatar != null)
            {
                var fileName = avatar.Headers.ContentDisposition.FileName;
                userDto.Avatar = fileName;
            }
            if (headerImage != null)
            {
                var fileName = headerImage.Headers.ContentDisposition.FileName;
                userDto.HeaderImage = fileName;
            }
            await _userService.UpdateAsync(userDto);

            return(Ok());
        }
 public async Task<IHttpActionResult> UploadFile(string username)
 {
     var isAllowed = ClaimsAuthorization.CheckAccess("Upload", "User", username);
     if (!isAllowed)
     {
         return StatusCode(HttpStatusCode.Unauthorized);
     }
     if (!Request.Content.IsMimeMultipartContent())
     {
         throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
     }
     var userDto = await _userService.GetSingleAsync(username);
     if (userDto == null) return NotFound();
     MultipartStreamProvider provider = new BlobStorageMultipartStreamProvider(userDto);
     await Request.Content.ReadAsMultipartAsync(provider);
     var avatar = provider.Contents.SingleOrDefault(p => p.Headers.ContentDisposition.Name.Contains("avatar"));
     var headerImage =
         provider.Contents.SingleOrDefault(p => p.Headers.ContentDisposition.Name.Contains("headerImage"));
     if (avatar != null)
     {
         var fileName = avatar.Headers.ContentDisposition.FileName;
         userDto.Avatar = fileName;
     }
     if (headerImage != null)
     {
         var fileName = headerImage.Headers.ContentDisposition.FileName;
         userDto.HeaderImage = fileName;
     }
     await _userService.UpdateAsync(userDto);
     return Ok();
 }