File uploading is a process where we send files from our computer to a server—in this case, running ASP.NET Core. File uploading in HTTP requires two things:
- You must use the POST verb.
- The multipart/form-data encoding must be set on the form.
Where ASP.NET Core is concerned, the included model binders know how to bind any posted files to an IFormFile object (or collection of objects). For example, take a form such as the following:
@using (Html.BeginForm("SaveForm", "Repository", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="Save"/>
}
You can retrieve the file in an action method such as this one:
[HttpPost("[controller]/[action]")]
public IActionResult SaveForm(IFormFile file)
{
var length = file.Length;
var name...