Powered By Blogger

Wednesday, October 28, 2009

retrieve file from database c#

public void retrieveFile()
{

string fileId = "10";



connection.Open();
SqlCommand command = new SqlCommand("SELECT flname,flcontent FROM upload WHERE fid = @FileId", connection);

command.Parameters.AddWithValue("@FileId", fileId);
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
if (reader.HasRows)
{
reader.Read();
byte[] content = reader["flcontent"] as byte[];
string filename = reader["flname"].ToString();
Response.Clear();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.AddHeader("Content-Length", content.Length.ToString());
Response.OutputStream.Write(content, 0, content.Length);
Response.End();
//writeByteArrayToFile(content, filename);
}



}