title: asp.net core upload file as part of a form
body: <p>Trying to upload a file as part of a form and store it in a folder in wwwroot then store the url for the file in a sql server database table along with the other details of the form? If anyone has any ideas it would be greatly appriciated. I have used this code but it does not store anything. However when i created a fresh new project i managed to get the code working on that correctly but cannot seem to get it to work in this work project. Not sure if it has something to do with the fact i have repositories and the fresh project i created that did work didnt have any repositories. That is the only difference i can think of. Any ideas?</p>
  
  <p>//Model</p>
  
  <pre><code>namespace PostProjectEvaluations.Web.Models
  {
      public partial class Projects
      {
          [Key]
          public int ProjectId { get; set; }
          [Required]
          [StringLength(300)]
          public string Name { get; set; }
          [Required]
          [StringLength(50)]
          public string Manager { get; set; }
  
          public string FilePath { get; set; }
  
      }
      public class ProjectsVM
      {
          public string Name { get; set; }
          public IFormFile File { get; set; }
      }
  </code></pre>
  
  <p>//Controller</p>
  
  <pre><code>    namespace PostProjectEvaluations.Web.Controllers
      {
          public class projectsController : Controller
          {
              private readonly IApplicationRepository ApplicationRepository;
              private readonly PostProjectEvaluationsContext _context;
              private IHostingEnvironment mxHostingEnvironment { get; set; }
              private object objproject;
  
              public projectsController(IApplicationRepository applicationRepository,
                  IHostingEnvironment hostingEnvironment, PostProjectEvaluationsContext context)
              {
                  mxHostingEnvironment = hostingEnvironment;
                  ApplicationRepository = applicationRepository;
                  _context = context;
              }
  
  
              public IActionResult Index()
              {
                  ViewBag.dataSource = ApplicationRepository.GetAllProjects().ToList();
                  var projects = ApplicationRepository.GetAllProjects();
                  return View(projects);
              }
  
              [HttpPost]
              [ValidateAntiForgeryToken]
              public async Task&lt;IActionResult&gt; Index(ProjectsVM projectsVM)
              {
                  if (projectsVM.File != null)
                  {
                      //upload files to wwwroot
                      var fileName = Path.GetFileName(projectsVM.File.FileName);
                      var filePath = Path.Combine(mxHostingEnvironment.WebRootPath, "Uploads", fileName);
  
  
                      using (var fileSteam = new FileStream(filePath, FileMode.Create))
                      {
                          await projectsVM.File.CopyToAsync(fileSteam);
                      }
                      //your logic to save filePath to database, for example
  
                      Projects projects = new Projects();
                      projects.Name = projectsVM.Name;
                      projects.FilePath = filePath;
  
                      _context.Projects.Add(projects);
                      _context.SaveChanges();
                  }
                  else
                  {
  
                  }
                  return View("Index");
              }
   public IActionResult Details(int id)
          {
              var project = ApplicationRepository.GetProjects(id);
              return View(project);
          }
  
          [HttpGet]
          public IActionResult Create()
          {
  
              var project = new Projects();
              return View(project);
          }
  
          [HttpPost]
          public IActionResult Create(Projects projects)
          {
              ApplicationRepository.Create(projects);
              return RedirectToAction("Index");
  
          }
  
  
  
          public IActionResult Delete(int id)
          {
              var project = ApplicationRepository.GetProjects(id);
              ApplicationRepository.Delete(project);
              return RedirectToAction("Index");
          }
  
  
          [HttpGet]
          public IActionResult Edit(int id)
          {
              var project = ApplicationRepository.GetProjects(id);
              //mxApplicationRepository.SaveChangesAsync();
              return View(project);
          }
          [HttpPost]
          public IActionResult Edit(Projects projects)
          {
              ApplicationRepository.Edit(projects);
              //mxApplicationRepository.SaveChangesAsync();
              return RedirectToAction("Index");
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>//View</p>
  
  <pre><code>      &lt;form enctype="multipart/form-data" asp-controller="Projects" asp-action="Create" method="post" class="form-horizontal"&gt;
              &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
              &lt;div class="form-body"&gt;
                  &lt;h3 class="form-section"&gt;Project Info&lt;/h3&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-6"&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label class="control-label col-md-3"&gt;Project Name&lt;/label&gt;
                              &lt;div class="col-md-9"&gt;
                                  &lt;input asp-for="Name" class="form-control" placeholder="Name"&gt;
                                  &lt;span asp-validation-for="Name" class="text-danger"&gt;&lt;/span&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;!--/span--&gt;
                      &lt;div class="col-md-6"&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label class="control-label col-md-3"&gt;Project Manager&lt;/label&gt;
                              &lt;div class="col-md-9"&gt;
                                  &lt;input asp-for="Manager" class="form-control" placeholder="Name"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;!--/span--&gt;
                  &lt;/div&gt;
  
                  &lt;!--/span--&gt;
              &lt;/div&gt;
              &lt;h3 class="form-section"&gt;Project Files&lt;/h3&gt;
              &lt;!--/row--&gt;
              &lt;div class="row"&gt;
                  &lt;div&gt;
                      &lt;div class="col-md-6"&gt;
                          &lt;div class="col-md-10"&gt;
                              &lt;p&gt;Upload one or more files using this form:&lt;/p&gt;
                              &lt;input type="file" name="files" multiple /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div&gt;
                  &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="form-actions right"&gt;
                      &lt;input type="submit" class="btn blue-assembly" name="submitButton" value="Save" /&gt;
                      &lt;a asp-action="Index" class="btn default" onclick="cancelClick()"&gt;Cancel&lt;/a&gt;
                  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>