ASP.NET Core MVC, Get file from database, and render as image
<p>I have image data being stored in a SQL table with a field type of <em>varbinary(max)</em>   I also store the image content type.</p>

<p>Using Microsoft ASP.NET Core MVC and Dapper, I am trying to get the file back from the database and render it as an image. </p>

<p>This is my <em>FileModel</em>:</p>

<pre><code>using System;

namespace Brand.Models
{
    public class FileModel
    {
        public Guid ID { get; set; }
        public string FileName { get; set; }
        public string FileType { get; set; }
        public byte[] FileData { get; set; }
        public int FileLength { get; set; }
        public long Version { get; set; }
        public string FileSource { get; set; }
        public DateTime Created { get; set; }

        public FileModel() 
        {
            FileLength = 0;
            Version = 1;
            Created = DateTime.Now;
        }
    }
}
</code></pre>

<p>These are my functions:</p>

<pre><code>public FileStreamResult GetFile(string FileID)
{
    return GetFile(new Guid(FileID));
}

public FileStreamResult GetFile(Guid FileID)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        DynamicParameters dynamicParameters = new DynamicParameters();
        dynamicParameters.Add(@"ID", FileID);
        FileModel result = connection.Query&lt;FileModel&gt;("GetFile", dynamicParameters, commandType: CommandType.StoredProcedure).FirstOrDefault();
        Stream stream = new MemoryStream(result.FileData);
        return new FileStreamResult(stream, result.FileType);
    }
}
</code></pre>

<p>I call this with this kind of syntax:</p>

<pre><code>&lt;img src="@fileExtensions.GetFile(content.FileID)" /&gt;
</code></pre>

<p>I am getting a 404 response when I try this, so obviously something is wrong with the way I am implementing this.</p>