title: How to create an IFormFile image file and send it to input element in view?
body: <p>I have a form with an input element in my view.</p>
  <pre><code>&lt;input asp-for=&quot;Image&quot; id=&quot;mainImageInput&quot; type=&quot;file&quot; onchange=&quot;readURL(this,'mainImage')&quot; accept=&quot;image/jpeg, image/png&quot; class=&quot;form-control&quot;/&gt;
  </code></pre>
  <p>I am storing all my images in a database as byte[]. How can I convert byte[] to IFormFile and send it to my view in the input element? And no, I am not asking how to display the image, I want to insert it directly into the input element as if the user has selected it using &quot;Browse&quot;.</p>
  <p>I tried writing this, but it doesn't work:</p>
  <pre><code>var ad = _context.Advertisements
              .Select(x =&gt; new {
                  Id = x.Id,
                  Title = x.Title,
                  Description = x.Description,
                  Price = x.Price,
                  Location = x.Location,
                  CategoryId = x.CategoryId,
                  ImageInBase64 = Convert.ToBase64String(x.Image),
                  UserId = x.UserId,
                  ImageInByteArray=x.Image
              })
              .FirstOrDefault(x=&gt;x.Id==id);
  
  IFormFile file = null;
  using (var stream = new MemoryStream(ad.ImageInByteArray))
  {
      file = new FormFile(stream, 0, ad.ImageInByteArray.Length, &quot;name&quot;, &quot;fileName&quot;);
  }
  
  var viewModel = new AdvertisementViewModel()
              {
                  Id = ad.Id,
                  Title = ad.Title,
                  Description = ad.Description,
                  Price = ad.Price,
                  Location = ad.Location,
                  CategoryId = ad.CategoryId,
                  ImageInBase64 = ad.ImageInBase64,
                  UserId = ad.UserId,
                  Image = file
              };
  return View(&quot;Edit&quot;,viewModel);
  </code></pre>
  <p>This is the action where I should receive my image after submiting my form, all other properties are there, but I am getting null for the Image.</p>
  <pre><code>        [HttpPost]
          [ValidateAntiForgeryToken]
          [Authorize]
          public async Task&lt;IActionResult&gt; Edit(AdvertisementViewModel viewModel) 
          {
              // viewmodel.Image is null, so my model state will always be invalid
              // need a workaround
  
              if (!ModelState.IsValid)
              {
                  return View(viewModel);
              }
          }
  </code></pre>