I am trying to implement a file upload to my existing project. I have a reports table within my database and it contains a FilePath column.
public string FilePath {get; set;}
I have an existing Save action that handles both new and existing entries:
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "AdminManager")]
public ActionResult Save(Report report, HttpPostedFileBase file)
{
if (!ModelState.IsValid)
{
var viewModel = new ReportFormViewModel
{
Report = report,
Members = _context.Members.ToList(),
Subjects = _context.Subjects.ToList()
};
return View("ReportForm", viewModel);
}
if (report.Id == 0)
_context.Reports.Add(report);
else
{
var reportInDb = _context.Reports.Single(e => e.Id == report.Id);
reportInDb.Name = report.Name;
reportInDb.MemberId = report.MemberId;
reportInDb.SubjectId = report.SubjectId;
reportInDb.Date = report.Date;
}
_context.SaveChanges();
return RedirectToAction("Index", "Report");
}
I have already changed the form view to take a file so all that needs updating is the controller. I tried to find examples online but they mostly implemented it into a create action rather then what I have.