File uploading in HTTP requires two things:
- You must use the
POST
verb - The
multipart/form-data
encoding must be used in 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, if you have a form of:
@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 like this one:
[HttpPost("[controller]/[action]")] public IActionResult SaveForm(IFormFile file) { var length = file.Length; var name = file.Name; //do something with the file return this.View(); }
But the HTML file upload specification, (https://www.w3.org/TR/2010/WD-html-markup-20101019/input.file.html), also mentions the possibility to submit multiple files at once--it is the multiple
attribute. In that case, you can just...