コード例 #1
0
        public async Task <IActionResult> AddPhoto(AddPhotoModel addPhotoModel)
        {
            if (addPhotoModel == null || !ModelState.IsValid)
            {
                return(View());
            }

            var file = addPhotoModel.Files.FirstOrDefault();

            if (file == null || file.Length == 0)
            {
                return(View());
            }

            var title   = addPhotoModel.Title;
            var ownerId = GetOwnerId();

            byte[] content;
            using (var fileStream = file.OpenReadStream())
            {
                using (var memoryStream = new MemoryStream())
                {
                    fileStream.CopyTo(memoryStream);
                    content = memoryStream.ToArray();
                }
            }

            if (!await photosRepository.AddPhotoAsync(title, ownerId, content))
            {
                return(StatusCode(StatusCodes.Status409Conflict));
            }

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public async Task <IActionResult> AddPhoto(PhotoToAddDto photo)
        {
            var content = Convert.FromBase64String(photo.Base64Content);
            var result  = await photosRepository.AddPhotoAsync(photo.Title, photo.OwnerId, content);

            if (!result)
            {
                return(Conflict());
            }
            return(NoContent());
        }