How to Save IFormFile to SQLServer FileStream Table
<p>So either no one has tried to do this yet or I am just not finding anything on it. The old way you would upload a file is this:</p>
<pre><code>public class FileStorage
{
    public string FileName { get; set; }
    public byte[] FileStore { get; set; }
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task&lt;ActionResult&gt; Create(HttpPostedFileBase file)
{
    FileStorage fileAttachment = new FileStorage();
 
    using (Stream inputStream = file.InputStream)
    {
        MemoryStream memoryStream = inputStream as MemoryStream;
 
        //Check to see if stream returned is already a MemoryStream
        if(memoryStream == null)
        {
            memoryStream = new MemoryStream();
            inputStream.CopyTo(memoryStream);
        }
                
        fileAttachment.FileStore = memoryStream.ToArray();
        fileAttachment.FileName = file.FileName;
    }
 
    if (ModelState.IsValid)
    {
        db.FileAttachment.Add(fileAttachment);
        await db.SaveChangesAsync();
        return RedirectToAction(&quot;Index&quot;);
    }
 
    return RedirectToAction(&quot;Index&quot;);
}
</code></pre>
<p>I know that the .Net Core uses <code>IFormFile</code> but all the resources I have found for saving it talk about saving it to the wwwroot folder on the web server. I am successfully passing the file to my controller but have not been able to figure out how to convert it to the <code>byte[]</code> to save to the DB <code>FileStream</code> table.</p>
<p>Here is my current Code:</p>
<pre><code>[HttpPost]
public async Task&lt;IActionResult&gt; Upload(IFormFile uploadFile)
{
    if (ModelState.IsValid)
    {
        var parsedContent = ContentDispositionHeaderValue.Parse(uploadFile.ContentDisposition);
        var fileName = Path.Combine(_hostingEnvironment.WebRootPath, &quot;Uploads&quot;, parsedContent.FileName.Trim('&quot;'));
        
        await uploadFile.SaveAsAsync(fileName);
        
    }

    return View(&quot;Index&quot;);
}
</code></pre>
<p>How do I convert it to work with the DB <code>FileStream</code></p>