public async Task <ActionResult> PostForm([FromForm] PersonCreationFormDTO personCreationFormDTO) { var person = mapper.Map <Person>(personCreationFormDTO); if (personCreationFormDTO.Picture != null) { using (var memoryStream = new MemoryStream()) { await personCreationFormDTO.Picture.CopyToAsync(memoryStream); var content = memoryStream.ToArray(); var extension = Path.GetExtension(personCreationFormDTO.Picture.FileName); person.Picture = await fileStorageService.SaveFile(content, extension, containerName, personCreationFormDTO.Picture.ContentType); } } context.Add(person); await context.SaveChangesAsync(); var personDTO = mapper.Map <PersonDTO>(person); return(CreatedAtRoute("GetPerson", new { id = personDTO.Id }, personDTO)); }
public async Task <ActionResult> Put(int id, [FromForm] PersonCreationFormDTO personCreationFormDTO) { var PersonDB = await context.Persons.FirstOrDefaultAsync(p => p.Id == id); //busca la persona if (PersonDB == null) { return(NotFound()); } var imgPerson = PersonDB.Picture; PersonDB = mapper.Map(personCreationFormDTO, PersonDB); //se actualiza mediante mapper y no ignora picture no se porque aunque se puso en AutoMapperProfile //da null la img si personCreationFormDTO es null ignorando la img de antes PersonDB, se corrige agregandole la img en otra variable if (personCreationFormDTO.Picture != null) { using (var memoryStream = new MemoryStream()) { await personCreationFormDTO.Picture.CopyToAsync(memoryStream); // lo copia en la variable memory stream var content = memoryStream.ToArray(); //convierte la imagen en []bytes var extension = Path.GetExtension(personCreationFormDTO.Picture.FileName); //coje desde el punto del nombre (img.png) = png PersonDB.Picture = await fileStorageService.EditFile(content, extension, containerName, PersonDB.Picture, personCreationFormDTO.Picture.ContentType); //se guarda la img en la carpeta etc } } else { PersonDB.Picture = imgPerson; } await context.SaveChangesAsync(); //return Ok(); var personDTO = mapper.Map <PersonDTO>(PersonDB); return(CreatedAtRoute("GetPerson", new { id = personDTO.Id }, personDTO)); }