Upload file in MVC 6 (ASP.NET Core 1.0)

There are many ways to upload files in MVC 6.

Below code is the simplest way I found till now, If I get any other simpler method then I will update this code.

view (cshtml) :

<form method="post" asp-action="Upload" asp-controller="Home" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="upload"/>

controller (cs) :

public IActionResult Upload(IFormFile file)
    if (file == null || file.Length == 0)
        throw new Exception("file should not be null");

    var originalFileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');


Happy coding!


Blog at WordPress.com.

Up ↑