Download file downloads the source page instead

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();
}



}
 
S

Simon Rigby

Hi Patrice,

Yes good point and in fact I removed it to check if an exception was
being raised, but unfortunately nothing was raised.

Many Thanks

Simon
 
S

Simon Rigby

Woops,

I discovered that my while condition was incorrect (should have been
while (dataToRead > 0).

Now I am getting the data of the file written to the stream, but it is
immediately followed by the content of the rendered page that the
functionality is contained by.

Any ideas

Simon
 
P

Patrice

Clear the markup if you don't need it or use Response.Clear or Response.End
once you are done to make sure nothing is rendered by the ASP.NET
infrastructure.
 
S

Simon Rigby

Clear the markup if you don't need it or use Response.Clear or Response.End
once you are done to make sure nothing is rendered by the ASP.NET
infrastructure.

"Simon Rigby" <[email protected]> a écrit dans le message de (e-mail address removed)...

Thanks Patrice,

Just found that and cam back to report the solution and found you
already had.

I change finally block to

stream.Close();
Response.Close();

Works a treat.

Many Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top