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.

4
  • 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? Commented Apr 1, 2018 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 Commented Apr 1, 2018 at 13:21
  • 4
    Uploading a File (Or Files) With ASP.NET MVC - 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 Commented Apr 1, 2018 at 13:22
  • Possible duplicate of Uploading Files into Database with ASP.NET MVC Commented Apr 1, 2018 at 13:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.