2

I've been searching the web now for a couple of hours. Although it seems very easy to upload a blob file to a sql database, it's a nightmare trying to download it again.

I have a gridview displaying records. The grid view has a linkbutton which I want to use to download a blob file wich is saved in the same table as where the gridview is loading its data from. I'm passing the records id to my code-behind function with the onclick event.

here is my code behind for the on click event

protected void Downloadbutton_Click(Object sender, CommandEventArgs e)
    {

        string reqid = e.CommandArgument.ToString();

        using (SqlConnection connection = new SqlConnection("ConnectionString"))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select Attached_File_Name from ABSENCE_REQUEST where Request_ID = @Request_ID";
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("@Request_ID", Convert.ToInt32(20057));

            byte[] buffer = (byte[]) cmd.ExecuteScalar();
            using (FileStream fs = new FileStream(@"C:\test.pdf", FileMode.Create))
            {
                fs.Write(buffer, 0, buffer.Length);
            }
        }
    }

I know that in my code I actually set a download location for the file. But how can I change it so the user will be asked where to save the file?

1 Answer 1

4

I think you can simply write the data to the Response (not tested), something like:

private void WriteFile (Byte[] bytes, string fileName)
{
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = dt.Rows[0]["ContentType"].ToString();
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}

You may also want to consider creating a generic handler for this, so your code would be pretty similar, but you can then have links to files rather than only available on button clicks, e.g. your link might be:

YourWebsite/Downloads/RequestFileHandler.ashx?RequestID=5

To download the file where the request_ID is 5

EDIT

Sorry, completely assumed this was for a web page, however have now realised it may not be, in which case you could use something like:

private bool SaveBytesToFile(byte[] butes)
{
    var saveFileDialog = new SaveFileDialog();
    if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
    {
        using (var fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write))
        {
            fileStream.Write(bytes, 0, bytes.Length);
        }
        return true;
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your proposed solution. I don't get any error message, but nothing happens after clicking the download button. I used a breakpoint and made sure the code actually gets the byte[] data and it does. but nothing happens on the front end... any ideas?
Is it a web app or a windows app?
It's a web app (actuall a web part for SharePoint), but the same rules. I just figured out why nothing happens for the on click event. The button is inside a grdiview wich is inside an asp:updatepanel. Just to test the theory I made a copy of the button and placed it outside the update panel on the page. That button works perfectly with your solution. Now I have a new problem... making that function work from within an updatepanel.

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.