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!