Forcing Download of File Appends HTML from Download Page to Downloaded File?

B

Brett Kelly

Ok, I know this sounds odd. Let me explain further.

I have an ASP.net page (w/ C# code behind) that, when given a session
variable containing the path to a local file, will attempt to start the
download of that file for the user.

Here's the content of my Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
if (Response.IsClientConnected)
{
string fname = Session["fn"].ToString();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment;filename=" +
fname);
Response.WriteFile(fname);
}
}


Somehow, this code will append the html/css code from the download page
to the end of the file being downloaded. For Example, say i'm trying
to download test.txt that contains one line:
----------------------------
This is a test
----------------------------

When i download the file, I get this:

----------------------------
This is a test


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Untitled Page
</title></head>
<body>
<form name="form1" method="post" action="sf.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJNzgzNDMwNTMzZGT/M1xVFtSmiLzmcScAG5lBHRlqGw==" />
</div>

<div></div>
</form>
</body>
</html>
-------------------------

Now, I've tried clearing the Response buffer before sending the file,
but no dice.

Any suggestions?
 
S

Steve C. Orr [MVP, MCSD]

Put a Response.Clear() before your code and a Response.End() after it.
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top