Exemplo n.º 1
0
        public async Task <ActionResult> UpdateProfilePhoto([FromForm] UpdateProfilePhotoRequest request)
        {
            var userId = User.Claims.First(a => a.Type == Constants.ClaimUserId).Value;
            var user   = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                throw new BusinessException("UserNotFound", "Kullanıcı bulunamadı.");
            }

            var folderPath = _environment.ContentRootPath + "\\Assets\\ProfilePictures\\";

            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            if (request.Photo.ContentType.Length > 30)
            {
                throw new Exception("Wrong Content-Type");
            }
            var fileName     = Guid.NewGuid() + "." + request.Photo.ContentType.Replace('/', '.');
            var fileFullPath = folderPath + fileName;

            using (FileStream fileStream = new FileStream(fileFullPath, FileMode.Create))
            {
                await request.Photo.CopyToAsync(fileStream);
            }

            user.PicturePath = fileName;
            await _userManager.UpdateAsync(user);

            return(Ok(new { user.PicturePath }));
        }
 public ActionResult UploadFileImage([FromForm] UpdateProfilePhotoRequest updateProfilePhotoRequest)
 {
     if (ModelState.IsValid)
     {
         var profile = UnitOfWork.ApplicationUserRepository.GetById(updateProfilePhotoRequest.Id);
         if (profile != null)
         {
             var imageName = UploadFile.UploadFileImage(updateProfilePhotoRequest.File, updateProfilePhotoRequest.Id);
             if (imageName != null)
             {
                 profile.Image = imageName;
                 UnitOfWork.Commit();
             }
             return(Ok(new BaseResponse()
             {
                 IsSuccess = true, Message = "Başarılı"
             }));
         }
     }
     return(Ok(ReturnValidationError()));
 }