示例#1
0
        public async Task <IActionResult> AddAvatar([FromForm] IFormFile image)
        {
            var UserId   = int.Parse(HttpContext.User.Identity.Name);
            var username = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var Path     = @"/profiles/" + username + @"/profile_avatar/";
            var RootPath = _webHostEnvironment.WebRootPath + Path;


            string[] files = Directory.GetFiles(RootPath);
            await Task.Run(() => DeleteFiles(files));

            await Task.Run(() => UploadFile(image, RootPath));

            var pathOnServer = "http://" + Request.Host.Value + Path + image.FileName;
            await Task.Run(() => _profileService.AddAvatar(UserId, pathOnServer));

            return(Ok(pathOnServer));
        }