Force Download Not Working

L

localhost

I have a text file that I need web users to get as a "forced download"
instead of displaying in the browser. The following Page_Load code is
not working, what did I miss?


private void Page_Load(object sender, System.EventArgs e)
{
string sendText = "[empty]";
if ( Session["sendText"] != null )
{
sendText = Session["sendText"].ToString();
}
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
byte[] sendBytes = asciiEncoding.GetBytes( sendText );
MemoryStream memoryStream = new MemoryStream( sendBytes );
Response.ClearContent();
Response.ClearHeaders();
//Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Length",
memoryStream.Length.ToString() );
Response.AddHeader("Content-Disposition", "attachment;
filename=bs.bad" );
//Response.Charset = "UTF-8";
Response.BinaryWrite(sendBytes);
Response.Flush();
Response.End();
}

If you run that, you will just see "[empty]" instead of a save dialog.

Help?
 
S

Steven Cheng[MSFT]

Hi Localhost,

As for the code snipet you provided, I've also run it in a simple page on
my side, but it seems that it works well and the file open/save dialog is
correctly popup. So I'm not sure whether the problem is due to the client
side. Would you try visiting the page through multi client machine to see
whether the behavior are all the same ? If some other machines can visit
it correctly, I think the problem may due to the particular machine.
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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top