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

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

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. Christopher D. Wiederspan

    Saving Downloaded Xml from ASPX page as File

    Christopher D. Wiederspan, Sep 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    447
    Christopher D. Wiederspan
    Sep 9, 2004
  2. Ryan Taylor
    Replies:
    2
    Views:
    1,308
    Ryan Taylor
    Nov 10, 2004
  3. Replies:
    4
    Views:
    586
  4. Gerrit Horeis

    Safari 2.0.0.4 appends .html to pdf files

    Gerrit Horeis, May 29, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    611
  5. Bogdan
    Replies:
    0
    Views:
    1,983
    Bogdan
    Sep 18, 2009
  6. dmiller23462

    Email AND File Appends

    dmiller23462, May 27, 2004, in forum: ASP General
    Replies:
    1
    Views:
    275
    Me Padre
    May 28, 2004
  7. Adam Akhtar
    Replies:
    9
    Views:
    834
    Florian Gilcher
    Aug 16, 2008
  8. Guy
    Replies:
    5
    Views:
    357
    Walt Mankowski
    Dec 10, 2003
Loading...