title: How to upload images to folder under wwwroot in .NET? (MVC)
body: <p>I am trying to save image file to a folder under wwwroot, the database should store the path name of the file, and the folder under wwwroot should store the image file. </p>
  
  <p>What I want to achieve is to save the image file to the folder under wwwroot.
  and
  the path name to the image file to be save to the local database.</p>
  
  <p>Thank you!!</p>
  
  <p>View:</p>
  
  <pre><code>&lt;form asp-controller="MobileSuits" asp-action="Add"&gt;
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;TheMobileSuit&lt;/h4&gt;
          &lt;hr /&gt;
          &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="Id" class="col-md-2 control-label"&gt;&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  &lt;input asp-for="Id" class="form-control" /&gt;
                  &lt;span asp-validation-for="Id" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="Name" class="col-md-2 control-label"&gt;&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  &lt;input asp-for="Name" class="form-control" /&gt;
                  &lt;span asp-validation-for="Name" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="PicFile" class="col-md-2 control-label"&gt;&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                    @*&lt;input type="file" name="photo"
                                 class="form-control" /&gt;*@
                  &lt;input type="file" name="file" id="file" /&gt; 
                  &lt;span asp-validation-for="PicFile" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>[Authorize]
          public IActionResult Add()
          {
              return View();
          }
  
      [Authorize]
      [HttpPost]
      public IActionResult Add(TheMobileSuit themobilesuits, IFormFile photo)
      {
  
          DbSet&lt;TheMobileSuit&gt; dbs = _dbContext.TheMobileSuit;
          dbs.Add(themobilesuits);
  
          if (_dbContext.SaveChanges() == 1)
              TempData["Msg"] = "New Order Added!";
          else
              TempData["Msg"] = "Error.";
          return RedirectToAction("Index");
      }
  </code></pre>