Last active
December 26, 2016 05:47
-
-
Save ahmettek/7aa55280ead91500b8f408424b0ff031 to your computer and use it in GitHub Desktop.
ASP.NET Core File Uplaod
This file contains 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
<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