Skip to content

Instantly share code, notes, and snippets.

@EliuTimana
Last active January 31, 2019 02:53
Show Gist options
  • Save EliuTimana/2291f41f1d285d62dee0812eb33216c8 to your computer and use it in GitHub Desktop.
Save EliuTimana/2291f41f1d285d62dee0812eb33216c8 to your computer and use it in GitHub Desktop.
[HttpPost("[action]")]
public async Task<IActionResult> UploadFile(IEnumerable<IFormFile> files)
{
foreach (var formFile in files)
{
// Archivos existentes
const string ruta = "/home/eliu/Downloads/qwerty.txt";
var mimeFile = MimeGuesser.GuessMimeType(ruta);
// Archivos que están siendo subidos al servidor
var mime = MimeGuesser.GuessMimeType(formFile.OpenReadStream());
var ext = MimeGuesser.GuessExtension(formFile.OpenReadStream());
//filtro de los archivos o mimes que deseas permitir
if (ext == "pdf" || ext == "png")
{
var filePath = Path.Combine(_env.WebRootPath, Guid.NewGuid() + ext);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
}
else
{
// Gestiona los errores
ModelState.AddModelError("Upload", $"{formFile.FileName} no Permitido");
}
}
return RedirectToAction("Index");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment