0

I'm trying to save an image in ASP.NET Core MVC using C#, in a folder called Images.

This is my code - Index view markup:

@using(Html.BeginForm("UploadFile","Upload", FormMethod.Post, new { 
 enctype="multipart/form-data"}))  
{ 
<div>  
    @Html.TextBox("file", "", new {  type= "file"}) <br />  

    <input type="submit" value="Upload" />  

    @ViewBag.Message  

</div>      
}

Controller:

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
        try
        {
            if (file.ContentLength > 0)
            {
                string _FileName = Path.GetFileName(file.FileName);
                string _path = Path.Combine(Directory.GetCurrentDirectory(), "Images", _FileName);
                file.SaveAs(_path);
            }

            ViewBag.Message = "File uploaded successfully!";

            return View();
        }
        catch
        {
            ViewBag.Message = "File upload failed!";
            return View();
        }
}

My problem is that ASP.NET Core MVC doesn't accept HttpPostedFileBase, VS Code community tells me that cannot find httpPostedFileBase.

Is there another way to do it or do I have to install a special nuget?

Thank you very much!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
F205
  • 93
  • 1
  • 1
  • 11
  • Thank you for your comment! It's say that there is no more httpPostedFileBase but when I try to use what it says there it doesn't work. – F205 Jul 27 '22 at 19:39
  • You will have to provide more information for us to help because simply saying you tried it, and it doesnt work is not enough for us to work with. There are several good responses to that question that explain how it works and what you need to change to get it to work. How did you update your code to adapt to what you learned? What is it doing differently than you expect? What errors might you be receiving? – Josh Mein Jul 27 '22 at 19:55

0 Answers0