response.write and plain/text

M

MarkMurphy

I have a simple export.aspx page that allows a user to fill in a form
to export some data. The postback logic writes the data to the
response stream. I have two small issues:

1) The data is tab-delimited text. If I set
Response.ContentType="plain/text", the format is fine, but an
Open/Save dialog will appear on the IE6 browser, which I don't want.
If don't set the ContentType at all, the dialog goes away, but the
browser removes the linefeeds from the output.

2) The browser history does not contain the original form display.
Hitting the back button skips over the form.

Thanks!

Mark
 
S

Steven Cheng[MSFT]

Hi Mark,

As for the two questions you mentioned, here are my understandings:

1. The ContentType is used to speicfy the mime type of the response stream.
When we don't set it , its the original value (text/html) for the aspx
page. So it'll display as html and omit those "tab" even line break flag.
And as for the text file, I think the correct ContentType should be
"text/plain" rather than "plain/text", and the "plain/text" may not be
recognized by the client browser so that it open it outside the browser via
other text editors. Please try changing the contenttype to "text/plain" and
see whether it works.

2. When we hit "back" button, the browser will retrieve the previous url
stored in the history list and load it.(normally from the client cache).
And generally, when we hit a button on asp.net page, it postback and
reutrn, then the original url(before postback) is added into history list(
you can push the dropdown button beside the "back" button to view the
history list). However, as for your situation, when you post back, and
return a txt data stream rather than text/html, then the original html
document still remain( didn't reload). So it is not added into history
list, thus the "back" button won't work (not history item stored for the
previous page ).

Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
M

MarkMurphy

Hey thanks Steven,

Actually, I miswrote. I was already specifying text/plain. Still the
receiving browser pulls up the save/open dialog.

Here is the simple case:

private void DisplayMessage(string msg)
{
//Clear existing stuff from response
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();

//Set response type
Response.ContentType = "text/plain";

//Write the message
Response.Write(msg);
//End the response causing it to be sent
Response.End();
}



-Mark
 
S

Steven Cheng[MSFT]

Hi Mark,

Thanks for your response. Really a bit surprised that since my test code is
exactly the same with yours. Anyway, I'll post my code here, you can also
have a try.

private void btnShowText_Click(object sender, System.EventArgs e)
{
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
Response.ContentType="text/plain";
//Response.AddHeader("content-disposition","inline;filename=data.txt");
//Response.WriteFile(Server.MapPath("./text.txt"));

string txt = "Index\tName\tEmail";
txt += "\n1\tSteven\[email protected]";
txt += "\n2\tMike\[email protected]";

Response.Write(txt);
Response.End();
}

# you can also try adding a such line
Response.AddHeader("content-disposition","inline;filename=data.txt");
in code to see whether it helps.

also, you can try using the
<a href="url of a txt file" target="_blank" > open text file</a>
hyperlink to open a txt file to see whether it can be opened in the browser
correctly.

Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top