Forcing Download of File Appends HTML from Download Page to Downloaded File?

Discussion in 'ASP .Net' started by Brett Kelly, Jun 16, 2006.

  1. Brett  Kelly

    Brett Kelly Guest

    Ok, I know this sounds odd. Let me explain further.

    I have an ASP.net page (w/ C# code behind) that, when given a session
    variable containing the path to a local file, will attempt to start the
    download of that file for the user.

    Here's the content of my Page_Load:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (Response.IsClientConnected)
    {
    string fname = Session["fn"].ToString();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition",
    "attachment;filename=" +
    fname);
    Response.WriteFile(fname);
    }
    }


    Somehow, this code will append the html/css code from the download page
    to the end of the file being downloaded. For Example, say i'm trying
    to download test.txt that contains one line:
    ----------------------------
    This is a test
    ----------------------------

    When i download the file, I get this:

    ----------------------------
    This is a test


    <!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></head>
    <body>
    <form name="form1" method="post" action="sf.aspx" id="form1">
    <div>
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
    value="/wEPDwUJNzgzNDMwNTMzZGT/M1xVFtSmiLzmcScAG5lBHRlqGw==" />
    </div>

    <div></div>
    </form>
    </body>
    </html>
    -------------------------

    Now, I've tried clearing the Response buffer before sending the file,
    but no dice.

    Any suggestions?
     
    Brett Kelly, Jun 16, 2006
    #1
    1. Advertising

  2. Put a Response.Clear() before your code and a Response.End() after it.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net


    "Brett Kelly" <> wrote in message
    news:...
    > Ok, I know this sounds odd. Let me explain further.
    >
    > I have an ASP.net page (w/ C# code behind) that, when given a session
    > variable containing the path to a local file, will attempt to start the
    > download of that file for the user.
    >
    > Here's the content of my Page_Load:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (Response.IsClientConnected)
    > {
    > string fname = Session["fn"].ToString();
    > Response.ContentType = "application/octet-stream";
    > Response.AddHeader("Content-Disposition",
    > "attachment;filename=" +
    > fname);
    > Response.WriteFile(fname);
    > }
    > }
    >
    >
    > Somehow, this code will append the html/css code from the download page
    > to the end of the file being downloaded. For Example, say i'm trying
    > to download test.txt that contains one line:
    > ----------------------------
    > This is a test
    > ----------------------------
    >
    > When i download the file, I get this:
    >
    > ----------------------------
    > This is a test
    >
    >
    > <!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></head>
    > <body>
    > <form name="form1" method="post" action="sf.aspx" id="form1">
    > <div>
    > <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
    > value="/wEPDwUJNzgzNDMwNTMzZGT/M1xVFtSmiLzmcScAG5lBHRlqGw==" />
    > </div>
    >
    > <div></div>
    > </form>
    > </body>
    > </html>
    > -------------------------
    >
    > Now, I've tried clearing the Response buffer before sending the file,
    > but no dice.
    >
    > Any suggestions?
    >
     
    Steve C. Orr [MVP, MCSD], Jun 16, 2006
    #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. Ryan Taylor
    Replies:
    2
    Views:
    1,027
    Ryan Taylor
    Nov 10, 2004
  2. Replies:
    4
    Views:
    499
  3. Gerrit Horeis

    Safari 2.0.0.4 appends .html to pdf files

    Gerrit Horeis, May 29, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    538
  4. Bogdan
    Replies:
    0
    Views:
    1,774
    Bogdan
    Sep 18, 2009
  5. Adam Akhtar
    Replies:
    9
    Views:
    597
    Florian Gilcher
    Aug 16, 2008
Loading...

Share This Page