1

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.

1
  • The sp_ prefix should be avoided. It is reserved by MS and means system procedure, not stored procedure. sqlperformance.com/2012/10/t-sql-queries/sp_prefix Personally I dislike all prefixes as they add nothing except unnecessary keystrokes and difficulty finding objects in a list. Commented Sep 15, 2016 at 15:38

1 Answer 1

3

You've misspelled "disposition" (forgot an i). It should be:

Response.AppendHeader("Content-Disposition", ("attachment; filename=" + strFilename));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.