0

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.

Usman Khan
  • 143
  • 1
  • 12
  • Are you actually going to store the file path instead of the file itself? And if so, are you going to store the local or the original file path? – GSerg Apr 01 '18 at 13:18
  • Ideally I would store the file itself, examples I have seen online used file path so I tried it that way. I would want it so I would be able to download the file after its been uploaded if need be. Thanks – Usman Khan Apr 01 '18 at 13:21
  • 4
    [Uploading a File (Or Files) With ASP.NET MVC](https://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/) - and just save the path in the database, or if you want to store the file itself in the db, refer [Uploading Files into Database with ASP.NET MVC](https://stackoverflow.com/questions/15106190/uploading-files-into-database-with-asp-net-mvc) –  Apr 01 '18 at 13:22
  • Possible duplicate of [Uploading Files into Database with ASP.NET MVC](https://stackoverflow.com/questions/15106190/uploading-files-into-database-with-asp-net-mvc) – Heretic Monkey Apr 01 '18 at 13:37

0 Answers0