473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Download file downloads the source page instead

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.ConnectionStrin gs["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();
}

}

Feb 19 '07 #1
5 1252
IMO never use an empty catch clause as it hides whatever error could
happens.

"Simon Rigby" <go****@simonrigby.co.uka écrit dans le message de news:
11**********************@j27g2000cwj.googlegroups. com...
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.ConnectionStrin gs["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();
}

}

Feb 19 '07 #2
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

Feb 19 '07 #3
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

Feb 19 '07 #4
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" <go****@simonrigby.co.uka écrit dans le message de news:
11**********************@k78g2000cwa.googlegroups. com...
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

Feb 19 '07 #5
On Feb 19, 12:59 pm, "Patrice" <http://www.chez.com/scribe/wrote:
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" <goo...@simonrigby.co.uka écrit dans le message de news:
1171889475.834660.116...@k78g2000cwa.googlegroups. com...


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
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.

Feb 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Brandon Walters | last post by:
I wrote a file download module for my website. The reason for the file download module is that my website downloads work on a credit based system. So I need to keep track of and limit daily...
12
by: Swede | last post by:
Hello, I am posting this rather lengthy description here in hope of getting at least some insight into a problem that is really hurting my business, since I have come to the conclusion that my...
6
by: Jatin | last post by:
Hey Guys I have a web application that allows users to download files. But the files are not hosted on the webserver. The files are stored on external servers and are referenced by a URL/URI....
5
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
13
by: bmurphy | last post by:
Last week after much searching, I found the answer to my problem in this newsgroup. I can't find the thread from which I got my solution, but I wanted to report back what worked. When the site...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
0
by: bill | last post by:
Hi All, I have built an application that allows a user to download a file from the server. I would like to come up with a solution that works with all html standard compliant browsers in...
1
by: moris7 | last post by:
Hi everyone, i tried a lot but i don't seem to be able to do anything conclusive ! Here what i want to do : LogIn in a webpage using this URL : ...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.