Last active
January 31, 2019 02:53
-
-
Save EliuTimana/2291f41f1d285d62dee0812eb33216c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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