title: ASP Core Razor - How to upload file to SQL database?
body: <p>I created an app for work a long time ago using ASP.NET. I am now rewriting the app using Razor pages (ASP.NET core). I apologize if the question is too simple or if it has been answered elsewhere. I've searched for days! I'm having difficulty uploading a file to a table. </p>
  
  <p>The model looks like this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  
  namespace FinplianceProject.Models
  {
      public partial class TblSdgs
      {
          public int IntSdgId { get; set; }
          public string StrSdgShort { get; set; }
          public string StrSdgLong { get; set; }
          public byte[] ImgSdgIconNormal { get; set; }
          public byte[] ImgSdgIconInverted { get; set; }
      }
  }
  </code></pre>
  
  <p>My razor page looks like this:</p>
  
  <pre><code>@page
  @model FinplianceProject.Pages.SDGs.EditModel
  
  @{
      ViewData["Title"] = "Edit";
  }
  
  &lt;h2&gt;Edit&lt;/h2&gt;
  
  &lt;h4&gt;TblSdgs&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-4"&gt;
          &lt;form method="post"  enctype="multipart/form-data" asp-controller="UploadFiles" asp-action="Index"&gt;
              &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
              &lt;input type="hidden" asp-for="TblSdgs.IntSdgId" /&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="TblSdgs.StrSdgShort" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="TblSdgs.StrSdgShort" class="form-control" /&gt;
                  &lt;span asp-validation-for="TblSdgs.StrSdgShort" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="TblSdgs.StrSdgLong" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="TblSdgs.StrSdgLong" class="form-control" /&gt;
                  &lt;span asp-validation-for="TblSdgs.StrSdgLong" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="TblSdgs.ImgSdgIconNormal" class="control-label"&gt;&lt;/label&gt;
                  &lt;input type="file" asp-for="TblSdgs.ImgSdgIconNormal" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="TblSdgs.ImgSdgIconInverted" class="control-label"&gt;&lt;/label&gt;
                  &lt;input type="file" asp-for="TblSdgs.ImgSdgIconInverted" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;input type="submit" value="Save" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;a asp-page="./Index"&gt;Back to List&lt;/a&gt;
  &lt;/div&gt;
  
  @section Scripts {
      @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
  }
  </code></pre>
  
  <p>The backend looks like this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.RazorPages;
  using Microsoft.AspNetCore.Mvc.Rendering;
  using Microsoft.EntityFrameworkCore;
  using FinplianceProject.Models;
  
  namespace FinplianceProject.Pages.SDGs
  {
      public class EditModel : PageModel
      {
          private readonly FinplianceProject.Models.DB_19456_ccsolutions1Context _context;
  
          public EditModel(FinplianceProject.Models.DB_19456_ccsolutions1Context context)
          {
              _context = context;
          }
  
          [BindProperty]
          public TblSdgs TblSdgs { get; set; }
  
          public async Task&lt;IActionResult&gt; OnGetAsync(int? id)
          {
              if (id == null)
              {
                  return NotFound();
              }
  
              TblSdgs = await _context.TblSdgs.FirstOrDefaultAsync(m =&gt; m.IntSdgId == id);
  
              if (TblSdgs == null)
              {
                  return NotFound();
              }
              return Page();
          }
  
          public async Task&lt;IActionResult&gt; OnPostAsync()
          {
              if (!ModelState.IsValid)
              {
                  return Page();
              }
  
              _context.Attach(TblSdgs).State = EntityState.Modified;
  
              try
              {
                  await _context.SaveChangesAsync();
              }
              catch (DbUpdateConcurrencyException)
              {
                  if (!TblSdgsExists(TblSdgs.IntSdgId))
                  {
                      return NotFound();
                  }
                  else
                  {
                      throw;
                  }
              }
  
              return RedirectToPage("./Index");
          }
  
          private bool TblSdgsExists(int id)
          {
              return _context.TblSdgs.Any(e =&gt; e.IntSdgId == id);
          }
      }
  }
  </code></pre>
  
  <p>The page displays OK with a file upload control that works, but when I click "Save", the files are not saved.</p>
  
  <p>Please help and thanks in advance!</p>