Sending text stream as text file from aspx page

S

Simon Rigby

Hi folks,

Apologies if this is not directly relevant but I was struggling to find
a group with the appropriate context. So as my problem is in an aspx
web site I thought I'd try here first. Please feel free to suggest a
more relevent group.

I have some code that I am using to build a stream (test text at this
stage, although eventually will come from a database query). I want to
have this available as a download without having to write the file to
disk on the server first.

Here is the code I have which sits behind a button with submit
behaviour set to true.

protected void btnExport_Click(object sender, EventArgs e) {
MemoryStream stream = new MemoryStream();
StreamWriter sw = new StreamWriter(stream);

sw.WriteLine("Test");
sw.WriteLine("Test Again");

sw.Flush();
sw.Close();

byte[] byteArray = stream.ToArray();
stream.Flush();
stream.Close();

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;
filename=test.txt");
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray);
}

This is the extract of the first dozen or so lines of the downloaded
file test.txt.

Test
Test Again


<!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><style type="text/css">
.ctl00_TreeView1_0 {
font-family:Arial;font-size:Small;text-decoration:none; }

</style></head>
<body bgcolor="#ffffcc">
<form name="aspnetForm" method="post" action="ContactSearch.aspx"
id="aspnetForm">
<div>

etc etc etc etc

As you can see my text appears at the top of the file, but the response
includes the rest of the page (I have not included the full text here
but it does contain everything including the large hashed page state
block).

Any ideas greatly appreciated.

Many Thanks

Simon
 
S

Simon Rigby

Your da man. Thanks David. Good days work. get yaself a beer (religion
permitting).

Simon
 
C

Cowboy \(Gregory A. Beamer\)

If you only wan to output the text, clear out all of the tags or end the
response. Otherwise, the tags will always render.
 
S

Simon Rigby

yup response.end did the trick

thanks
If you only wan to output the text, clear out all of the tags or end the
response. Otherwise, the tags will always render.


Simon Rigby said:
Hi folks,

Apologies if this is not directly relevant but I was struggling to find
a group with the appropriate context. So as my problem is in an aspx
web site I thought I'd try here first. Please feel free to suggest a
more relevent group.

I have some code that I am using to build a stream (test text at this
stage, although eventually will come from a database query). I want to
have this available as a download without having to write the file to
disk on the server first.

Here is the code I have which sits behind a button with submit
behaviour set to true.

protected void btnExport_Click(object sender, EventArgs e) {
MemoryStream stream = new MemoryStream();
StreamWriter sw = new StreamWriter(stream);

sw.WriteLine("Test");
sw.WriteLine("Test Again");

sw.Flush();
sw.Close();

byte[] byteArray = stream.ToArray();
stream.Flush();
stream.Close();

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;
filename=test.txt");
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray);
}

This is the extract of the first dozen or so lines of the downloaded
file test.txt.

Test
Test Again


<!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><style type="text/css">
.ctl00_TreeView1_0 {
font-family:Arial;font-size:Small;text-decoration:none; }

</style></head>
<body bgcolor="#ffffcc">
<form name="aspnetForm" method="post" action="ContactSearch.aspx"
id="aspnetForm">
<div>

etc etc etc etc

As you can see my text appears at the top of the file, but the response
includes the rest of the page (I have not included the full text here
but it does contain everything including the large hashed page state
block).

Any ideas greatly appreciated.

Many Thanks

Simon
 
C

Cowboy \(Gregory A. Beamer\)

NP! :)


Simon Rigby said:
yup response.end did the trick

thanks
If you only wan to output the text, clear out all of the tags or end the
response. Otherwise, the tags will always render.


Simon Rigby said:
Hi folks,

Apologies if this is not directly relevant but I was struggling to find
a group with the appropriate context. So as my problem is in an aspx
web site I thought I'd try here first. Please feel free to suggest a
more relevent group.

I have some code that I am using to build a stream (test text at this
stage, although eventually will come from a database query). I want to
have this available as a download without having to write the file to
disk on the server first.

Here is the code I have which sits behind a button with submit
behaviour set to true.

protected void btnExport_Click(object sender, EventArgs e) {
MemoryStream stream = new MemoryStream();
StreamWriter sw = new StreamWriter(stream);

sw.WriteLine("Test");
sw.WriteLine("Test Again");

sw.Flush();
sw.Close();

byte[] byteArray = stream.ToArray();
stream.Flush();
stream.Close();

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;
filename=test.txt");
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray);
}

This is the extract of the first dozen or so lines of the downloaded
file test.txt.

Test
Test Again


<!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><style type="text/css">
.ctl00_TreeView1_0 {
font-family:Arial;font-size:Small;text-decoration:none; }

</style></head>
<body bgcolor="#ffffcc">
<form name="aspnetForm" method="post" action="ContactSearch.aspx"
id="aspnetForm">
<div>

etc etc etc etc

As you can see my text appears at the top of the file, but the response
includes the rest of the page (I have not included the full text here
but it does contain everything including the large hashed page state
block).

Any ideas greatly appreciated.

Many Thanks

Simon
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top