<p>I am currently working with large files and a MsSQL database. Storing the whole file as a byte array is time consuming an will blow up your database.</p>

<p>Consider using a SqlFileStream
<a href="https://msdn.microsoft.com/de-de/library/gg471497.aspx" rel="nofollow">https://msdn.microsoft.com/de-de/library/gg471497.aspx</a>
you have to do some configuration, but it will enable you to store any size of file within no time.</p>

<p>This will give you a way to stream the file directly from the database instead of reading the whole file in your asp server and then send it to the Client.</p>

<p>Microsoft Technical letter: To blob or not to blob:
<a href="https://arxiv.org/ftp/cs/papers/0701/0701168.pdf" rel="nofollow">https://arxiv.org/ftp/cs/papers/0701/0701168.pdf</a></p>
Download file stored in SQL database from C#
<p>I have SQL database where I am storing user uploaded file.</p>

<p>I have created a <code>linkbutton</code> to download the file. On click of that link button I am calling below code. Unfortunately it is not working. Not event throwing any error.</p>

<p>I have stored <strong>filename</strong>, <strong>contentType</strong> and <strong>bytes</strong> as 3 columns in my SQL table</p>

<pre><code>Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
</code></pre>

<p>What could be the issue? Or Any other way to achieve this?</p>

<p><strong>UPDATE:</strong></p>

<p>With same code and changes based on answer marked as correct, I did changes and it worked. Providing working code here for future reference of other users:</p>

<pre><code>    protected void Page_Load(object sender, EventArgs e)
    {
            DataTable dt = Session["fileAttachment"] as DataTable;
            string fileName = Convert.ToString(dt.Rows[0]["filename"]);
            string contentType = Convert.ToString(dt.Rows[0]["contentType"]);
            byte[] bytes = Convert.FromBase64String(Convert.ToString(dt.Rows[0]["bytearr"]));
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = contentType;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            Response.BinaryWrite(bytes); //
            //Response.BufferOutput = true;
            //Response.OutputStream.Write(bytes, 0, bytes.Length);
            Response.Flush();
            Response.End();
    }
</code></pre>

<p>Added above code in new page and redirected to this page after created session</p>