Force Download Not Working

Discussion in 'ASP .Net' started by localhost, Jun 29, 2004.

  1. localhost

    localhost Guest

    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?
     
    localhost, Jun 29, 2004
    #1
    1. Advertising

  2. 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
     
    Steven Cheng[MSFT], Jun 30, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PJS

    Force Download - XML problem

    PJS, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    520
  2. Kele
    Replies:
    0
    Views:
    2,805
  3. HaRoOn
    Replies:
    1
    Views:
    708
    scripts.contact
    Apr 19, 2007
  4. Navin
    Replies:
    1
    Views:
    109
    Atrax
    Aug 28, 2003
  5. Navin
    Replies:
    1
    Views:
    177
    Grant Wagner
    Aug 28, 2003
Loading...

Share This Page