473,406 Members | 2,894 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,406 software developers and data experts.

Sending text stream as text file from aspx page

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
5 2051
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
Your da man. Thanks David. Good days work. get yaself a beer (religion
permitting).

Simon

Oct 7 '06 #3
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
0
by: Ricardo Magalhaes | last post by:
Hi, I have an ASPX page and I need to call an ASP page sending parameters like Name, Adress, ie. using POST method. This next ASP page show payment informations.. If was from an ASP to...
3
by: sohan | last post by:
hi I have hyperlink column in a datagrid. The column contains the name of a text file. I am able to appendthe full path of the file. The file is on D drive on the server. But on clicking on...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
6
by: Rushwire | last post by:
Does anybody know how to send a meeting request using an ics/vcs (VCalendar) attachment from an asp.net page. I don't want my users to have to double click on the attachment but rather that it is...
4
by: SergioT | last post by:
Hi I got a sql's table with images and i need to show the image on a popup with some text, my problem is that the text did not show up only the image '------------Show the image ( works fine)...
0
by: sanjaygupta11 | last post by:
I am using httpwebrequest and httpwebresponse objects for sending data to remote appication by post and receiving the response from there. I am using this code in my windows application which will...
3
by: JJ297 | last post by:
Hello, After a user enters their request I'm sending a generated email to someone. How do I get that question to appear in the email? I have the link appearing in the email. This is what I...
0
by: silverrock7 | last post by:
Hello Friends i m stuck up to a place wherein i want to print a report which is displaying on a reportviewer. I actually want to send the report to printer directly without viewing it. i am...
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...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.