Skip to content

Instantly share code, notes, and snippets.

@ahmettek
Last active December 26, 2016 05:47
Show Gist options
  • Save ahmettek/7aa55280ead91500b8f408424b0ff031 to your computer and use it in GitHub Desktop.
Save ahmettek/7aa55280ead91500b8f408424b0ff031 to your computer and use it in GitHub Desktop.
ASP.NET Core File Uplaod
<form method="post" asp-action="Upload" asp-controller="Home" enctype="multipart/form-data">
<input type="file" name="files" multiple/>
<input type="submit" value="Upload" />
</form>
public class HomeController : Controller
{
private IHostingEnvironment _environment;
public HomeController(IHostingEnvironment environment)
{
_environment = environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Upload(ICollection<IFormFile> files)
{
var uploads = Path.Combine(_environment.WebRootPath, "uploads");
foreach (var file in files)
{
if (file.Length > 0)
{
using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
}
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment