By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,482 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

Sending text stream as text file from aspx page

P: n/a
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

Oct 7 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Try adding a Response.End to the last line of the function.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Simon Rigby" <go****@simonrigby.co.ukwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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

Oct 7 '06 #2

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

Simon

Oct 7 '06 #3

P: n/a
If you only wan to output the text, clear out all of the tags or end the
response. Otherwise, the tags will always render.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/

*************************************************
Think Outside the Box!
*************************************************
"Simon Rigby" <go****@simonrigby.co.ukwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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

Oct 7 '06 #4

P: n/a
yup response.end did the trick

thanks

Cowboy (Gregory A. Beamer) wrote:
If you only wan to output the text, clear out all of the tags or end the
response. Otherwise, the tags will always render.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/

*************************************************
Think Outside the Box!
*************************************************
"Simon Rigby" <go****@simonrigby.co.ukwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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
Oct 7 '06 #5

P: n/a
NP! :-)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/

*************************************************
Think Outside the Box!
*************************************************
"Simon Rigby" <go****@simonrigby.co.ukwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
yup response.end did the trick

thanks

Cowboy (Gregory A. Beamer) wrote:
>If you only wan to output the text, clear out all of the tags or end the
response. Otherwise, the tags will always render.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/

*********************************************** **
Think Outside the Box!
*********************************************** **
"Simon Rigby" <go****@simonrigby.co.ukwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
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

Oct 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.