I'm using ASP.NET Core 3.1. Currently, I am trying to send a image from my Project/Views/Admin/AddProduct.cshtml
file. I've created a form that contains a file input, and submit. I need to send the file as IFormFile to the UploadFile action method.
AddProduct.cshtml:
<form method="post" enctype="multipart/form-data" asp-controller="Admin" asp-action="UploadFile">
<input name="file" type="file" class="form-control" />
<input type="submit" value="Create" class="btn btn-default" />
</form>
Controllers/AdminController.cs:
[HttpPost]
public IActionResult UploadFile(IFormFile file)
{
System.Diagnostics.Debug.WriteLine($"SUCCESS: {file.FileName}");
return RedirectToAction("Index");
}
Whenever I click the submit button, the website redirects me to Admin/UploadFile, however, displaying: This site can’t be reached. localhost refused to connect. ERR_CONNECTION_REFUSED
I've tried:
- Using
HttpPostedFileBase
instead ofIFormFile
. From what I understood, it doesn't exist in .NET Core. - Passing it through a custom class
- Adding
[Consumes("multipart/form-data")]
toUploadFile
Any help is appreciated. I'd assume I'm doing something very wrong or I need to add something to StartUp.