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