S
Simon Rigby
Hi folks,
I have this code that selects a BLOB from a sql 2005 database sends it
to the browser as a download. Unfortunately the resulting file
contains the rendered page that the download operation is in as
opposed to the file. Can anyone see what is happening?
protected void gvAttachments_RowCommand(object sender,
GridViewCommandEventArgs e) {
SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["CnString"].ConnectionString);
SqlCommand cmd = new SqlCommand("procSelectMessageAttachment", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id",
int.Parse(e.CommandArgument.ToString()));
cn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
dr.Read();
Byte[] b = new byte[dr.GetBytes(0, 0, null, 0, Int32.MaxValue)];
dr.GetBytes(0, 0, b, 0, b.Length);
string filename = dr.GetString(1);
dr.Close();
cn.Close();
MemoryStream stream = new MemoryStream(b);
try {
Byte[] buffer = new byte[10000];
int dataToRead = (int)stream.Length;
int length = 0;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" +
filename);
while (dataToRead < 0) {
if (Response.IsClientConnected) {
length = stream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
buffer = new byte[10000];
dataToRead -= length;
} else { // prevent infinite loop if disconnected
dataToRead = -1;
}
}
} catch (Exception) {
} finally {
stream.Close();
}
}
I have this code that selects a BLOB from a sql 2005 database sends it
to the browser as a download. Unfortunately the resulting file
contains the rendered page that the download operation is in as
opposed to the file. Can anyone see what is happening?
protected void gvAttachments_RowCommand(object sender,
GridViewCommandEventArgs e) {
SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["CnString"].ConnectionString);
SqlCommand cmd = new SqlCommand("procSelectMessageAttachment", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id",
int.Parse(e.CommandArgument.ToString()));
cn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
dr.Read();
Byte[] b = new byte[dr.GetBytes(0, 0, null, 0, Int32.MaxValue)];
dr.GetBytes(0, 0, b, 0, b.Length);
string filename = dr.GetString(1);
dr.Close();
cn.Close();
MemoryStream stream = new MemoryStream(b);
try {
Byte[] buffer = new byte[10000];
int dataToRead = (int)stream.Length;
int length = 0;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" +
filename);
while (dataToRead < 0) {
if (Response.IsClientConnected) {
length = stream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
buffer = new byte[10000];
dataToRead -= length;
} else { // prevent infinite loop if disconnected
dataToRead = -1;
}
}
} catch (Exception) {
} finally {
stream.Close();
}
}