0

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 of IFormFile. From what I understood, it doesn't exist in .NET Core.
  • Passing it through a custom class
  • Adding [Consumes("multipart/form-data")] to UploadFile

Any help is appreciated. I'd assume I'm doing something very wrong or I need to add something to StartUp.

Community
  • 1
  • 1
Bubval
  • 1
  • 1
  • This error may do not caused by your shared code.Please check [this](https://forums.asp.net/t/2144963.aspx?getting+error+This+site+can+t+be+reached+localhost+refused+to+connect+). – Rena Mar 26 '20 at 01:45

1 Answers1

0

Rena's answer lead me to deleting my .vs folder and it actually fixed it. However, trying to use the function again, several hours later, resulted in the same error. Tried deleting the .vs folder again, but it didn't work.

A thing I failed to mention is that I am working on this project with a friend. We are using git and even though .gitignore is supposedly set up the correct way, recommended by Microsoft, and despite .vs being on the .gitignore, there has apparently been some sort of conflict that I cannot resolve easily.

So, I just told my friend to go to my branch, try my function, and it works for him. It's definitely the .vs folder. As Rena pointed out, I'd suggest checking this thread and these two links from stackoverflow: Link 1, Link 2

Bubval
  • 1
  • 1