Estoy programando un proyecto donde hay una sección de noticias, la noticia debe incluir una foto principal y una sección de galeria debajo del cuerpo de la noticia, ya tengo hecho el CRUD de la noticia con la foto principal, pero en la opción de añadir la galeria tengo un input multiple, me gustaría si alguien podría explicarme para subir los archivos multiples
Código html
<div class="input-group" style=" margin-top: 2%; margin-bottom: 2%;">
<input type="file" asp-for="Foto" name="Foto lang=" es"" accept="image/jpeg" class="custom-file-input" id="inputGroupFile04">
<label class="custom-file-label" for="inputGroupFile04">Subir foto principal</label>
<small id="name" class="form-text text-muted">(formato .jpg)</small>
</div>
<div class="input-group" style="margin-bottom: 4%;">
<input type="file" asp-for="Fotos" name="Fotos" lang="es" class="custom-file-input" id="inputGroupFile04" multiple>
<label class="custom-file-label" for="inputGroupFile04">Subir galería de fotos</label>
<small id="name" class="form-text text-muted">(formato .jpg)</small>
Repositorio
public void GuardarArchivo(int id, IFormFile archivo, string path)
{
var ruta = Path.Combine(path, "noticias", id + ".jpg");
FileStream fs = File.Create(ruta);
archivo.CopyTo(fs);
fs.Close();
}
ViewModel
public int idNoticias { get; set; }
public string Encabezado { get; set; }
public DateTime Fecha { get; set; }
public string Autor { get; set; }
public string DescripcionCorta { get; set; }
public string Cuerpo { get; set; }
public string VideoURL { get; set; }
[NotMapped]
public IFormFile Foto { get; set; }
Controlador
[HttpPost]
public IActionResult AgregarNoticia(NoticiasViewModel n)
{
if (ModelState.IsValid)
{
try
{
NoticiasRepository noticiasRepository = new NoticiasRepository();
var notic = noticiasRepository.GetNoticiasByNombre(n.Encabezado);
if (notic == null)
{
noticiasRepository.Insert(n);
if (n.Foto == null)
{
noticiasRepository.SetNoPhoto(n.idNoticias, $"{Environment.WebRootPath}");
}
else if (n.Foto.ContentType != "image/jpeg")
{
ModelState.AddModelError("", "Solo se pueden cargar imagenes JPEG.");
return View(n);
}
else if (n.Foto.Length > 1024 * 1024)
{
ModelState.AddModelError("", "El tamaño maximo de una imagen es de [ 1 MB ].");
return View(n);
}
else
{
noticiasRepository.GuardarArchivo(n.idNoticias, n.Foto, $"{Environment.WebRootPath}");
}
noticiasRepository.GuardarArchivo(n.idNoticias, n.Foto, $"{Environment.WebRootPath}");
return RedirectToAction("Noticias", "Administrador");
}
else
{
ModelState.AddModelError("", "Ya existe una noticia con este nombre");
return View(n);
}
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
return View(n);
}
}
else
{
return View(n);
}
}
El crud funciona con la imagen principal y se guardan las imagenes en una carpeta llamada noticias, cada imagen se guarda con el id de la noticia publicada