I'm trying to follow the dotnet core example here:
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads
with a few minor adjustments on the controller side. The following is what my form looks like:
<form method="post" enctype="multipart/form-data" asp-controller="Test" asp-action="DoUpload"><div class="form-group"><div class="col-md-10"><p>Upload one or more files using this form:</p><input type="file" name="files" multiple /></div></div><div class="form-group"><div class="col-md-10"><input type="submit" value="Upload" /></div></div>
Things to note: the asp-controller and asp-action is just changed to direct the form to a different controller. Additionally the name of the only input is "files". The enctype is set to "multipart/form-data".
So the corresponding controller code is simply the following:
public JsonResult DoUpload (List<IFormFile> files) { return Json(files.Count); }
So the expectation is it just returns the number of files submitted - this never seems to work. I've tried many different variations, but nothing seems to work. I tried the following:
//public JsonResult DoUpload([FromForm] Object obj, [FromForm] IFormFile files) { <-- Model bound complex types must not be abstract or value types and must have a parameterless constructor. //public async Task<IActionResult> Post(List<IFormFile> files) <- files is always empty //public JsonResult DoUpload(IFormFile files) { <- Model bound complex types must not be abstract or value types and must have a parameterless constructor. //public JsonResult DoUpload(UploadedDocument files) { <- attachment attribute, which is IFormFile is null //public JsonResult DoUpload ( [FromBody] UploadedDocument files) { <- 415 Unsupported Media Type //public JsonResult DoUpload ( [FromForm] UploadedDocument files) { <- {"attachment":null} //public JsonResult DoUpload (UploadedDocument files, IFormFile doc) { <- Model bound complex types must not be abstract or value types and must have a parameterless constructor. //public JsonResult DoUpload (List<IFormFile> files ) { <- files is null //public JsonResult DoUpload (Object files) { <- returns {} when (attempting to return Json(files))
Can anyone see what I'm doing wrong? I'm really sorry if there is a simple fix, but I can't seem to find a solution or help anywhere else online