コード例 #1
0
        public async Task <IActionResult> Save(SuperHeroDTO superhero)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Error"));
            }

            try
            {
                var heroSave = string.IsNullOrEmpty(superhero.Id) ? new SuperHero() : await _repository.GetByIdAsync <SuperHero>(superhero.Id);

                heroSave.Description = superhero.Description;
                heroSave.Name        = superhero.Name;

                await _repository.SaveOrUpdateAsync(heroSave, superhero.Id);

                var content  = superhero.Picture.Split(",")[1];
                var fileName = heroSave.Id;

                var path = Path.Combine(_appEnvironment.WebRootPath, @"images/superheroes") + $@"/{fileName + ".jpg"}";
        #if DEBUG
                path = Path.Combine(_appEnvironment.WebRootPath, @"images\superheroes") + $@"\{fileName + ".jpg"}";
        #endif
                System.IO.File.Delete(path);

                using (FileStream SourceStream = System.IO.File.Open(path, FileMode.OpenOrCreate))
                {
                    SourceStream.Seek(0, SeekOrigin.End);
                    await SourceStream.WriteAsync(Convert.FromBase64String(content), 0, Convert.FromBase64String(content).Length);
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception)
            {
                return(RedirectToAction("Error"));
            }
        }
コード例 #2
0
        public async Task <IActionResult> NewHero([FromBody] SuperHeroDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }

            try
            {
                var heroSave = new SuperHero()
                {
                    Description = model.Description,
                    Name        = model.Name
                };

                await _repository.SaveOrUpdateAsync(heroSave, model.Id);

                var content = model.Picture.Split(",")[1];

                var path = Path.Combine(_appEnvironment.WebRootPath, @"images/superheroes") + $@"/{heroSave.Id + ".jpg"}";
        #if DEBUG
                path = Path.Combine(_appEnvironment.WebRootPath, @"images\superheroes") + $@"\{heroSave.Id + ".jpg"}";
        #endif
                System.IO.File.Delete(path);

                using (FileStream SourceStream = System.IO.File.Open(path, FileMode.OpenOrCreate))
                {
                    SourceStream.Seek(0, SeekOrigin.End);
                    await SourceStream.WriteAsync(Convert.FromBase64String(content), 0, Convert.FromBase64String(content).Length);
                }

                return(Ok());
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }