I am trying to use a gridview in .net to allow users to download files from a repository stored in a sql server table as varbinary(max). The upload process was no problem. The problem is with retrieval. I use a linkbutton on each row in a gridview to allow downloading of the files. When the "download" linkbutton is clicked, I am presented with a save/open box (IE). The name of the file to be saved, however, is the name of the .aspx page, not the filename stored in the table.
Here's the code for the linkbutton click event:
protected void lbtnDownload_Click(object sender, EventArgs e)
{
int gmsrId = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string strFilename = String.Empty;
string strContentType = String.Empty;
string strConnString = dbUtilities.GetConnectionString();
using (SqlConnection sqlCon = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_SelectDocument";
cmd.Connection = sqlCon;
cmd.Parameters.AddWithValue("@intID", gmsrId);
sqlCon.Open();
using (SqlDataReader sdrDocument = cmd.ExecuteReader())
{
sdrDocument.Read();
bytes = (byte[])sdrDocument["gmsr_Document"];
strContentType = sdrDocument["gmsr_ContentType"].ToString();
strFilename = sdrDocument["gmsr_Filename"].ToString();
lblMessage.Text = "Filename = " + strFilename;
sqlCon.Close();
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = strContentType;
Response.AppendHeader("Content-Dispositon", ("attachment; filename=" + strFilename));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}
I am new to storing/retrieving blobs from a table and am getting close to just storing the files in the file system, but I hate to give up. I think I'm missing something small, but can't find it. Thanks in advance.