Downloading file to client

Discussion in 'ASP .Net' started by Cathryn Johns, Oct 26, 2005.

  1. Hi

    I'm trying to download some content as a file to the client. My code
    behind looks like this:

    private void DownloadFile(byte[] contents)
    {
    Response.ContentType = "text/csv";
    Response.AppendHeader("content-disposition",
    "attachment;filename=myFile.csv");
    Response.AppendHeader("content-length",contents.Length.ToString());
    Response.BinaryWrite(contents);
    }

    It works to an extent, in that it pops up the file download dialog and
    writes a file to the user's selected path. But the problem is what's
    being written: the first part of the file is the byte array that I'm
    trying to write, but the rest is my page's html (see below for sample
    output). I thought that setting the content-length header to the
    length of the byte array would force it to only write that many bytes,
    but it still writes 1.12Kb regardless.

    I just don't know what else to try; any ideas would be appreciated.

    Sample output (the HIPQ is the contents of the byte array):
    HIPQ
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>ViewAllocations</title>

    ..
    ..
    ..
    etc.
    Cathryn Johns, Oct 26, 2005
    #1
    1. Advertising

  2. Cathryn  Johns

    Chris Botha Guest

    After Response.BinaryWrite add the statement
    Response.End()

    "Cathryn Johns" <> wrote in message
    news:...
    > Hi
    >
    > I'm trying to download some content as a file to the client. My code
    > behind looks like this:
    >
    > private void DownloadFile(byte[] contents)
    > {
    > Response.ContentType = "text/csv";
    > Response.AppendHeader("content-disposition",
    > "attachment;filename=myFile.csv");
    > Response.AppendHeader("content-length",contents.Length.ToString());
    > Response.BinaryWrite(contents);
    > }
    >
    > It works to an extent, in that it pops up the file download dialog and
    > writes a file to the user's selected path. But the problem is what's
    > being written: the first part of the file is the byte array that I'm
    > trying to write, but the rest is my page's html (see below for sample
    > output). I thought that setting the content-length header to the
    > length of the byte array would force it to only write that many bytes,
    > but it still writes 1.12Kb regardless.
    >
    > I just don't know what else to try; any ideas would be appreciated.
    >
    > Sample output (the HIPQ is the contents of the byte array):
    > HIPQ
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    > <HTML>
    > <HEAD>
    > <title>ViewAllocations</title>
    >
    > .
    > .
    > .
    > etc.
    >
    Chris Botha, Oct 26, 2005
    #2
    1. Advertising

  3. Cathryn  Johns

    HIK Guest

    I've found it best to open the file in a new page.
    i.e. Response.redirect("Downloadpage.aspx",true)

    I put the download code in the new page dedicated just for this purpose.




    Chris Botha wrote:
    > After Response.BinaryWrite add the statement
    > Response.End()
    >
    > "Cathryn Johns" <> wrote in message
    > news:...
    >
    >>Hi
    >>
    >>I'm trying to download some content as a file to the client. My code
    >>behind looks like this:
    >>
    >>private void DownloadFile(byte[] contents)
    >>{
    >> Response.ContentType = "text/csv";
    >> Response.AppendHeader("content-disposition",
    >>"attachment;filename=myFile.csv");
    >> Response.AppendHeader("content-length",contents.Length.ToString());
    >> Response.BinaryWrite(contents);
    >>}
    >>
    >>It works to an extent, in that it pops up the file download dialog and
    >>writes a file to the user's selected path. But the problem is what's
    >>being written: the first part of the file is the byte array that I'm
    >>trying to write, but the rest is my page's html (see below for sample
    >>output). I thought that setting the content-length header to the
    >>length of the byte array would force it to only write that many bytes,
    >>but it still writes 1.12Kb regardless.
    >>
    >>I just don't know what else to try; any ideas would be appreciated.
    >>
    >>Sample output (the HIPQ is the contents of the byte array):
    >>HIPQ
    >><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >><HTML>
    >><HEAD>
    >><title>ViewAllocations</title>
    >>
    >>.
    >>.
    >>.
    >>etc.
    >>

    >
    >
    >
    HIK, Oct 26, 2005
    #3
  4. Cathryn  Johns

    CJ Guest

    That worked beautifully, when the method was called from an asp.net
    button click. Now it has to be called from a javascript menu system,
    so I've put the method on the OnPageLoad handler.

    Response.End doesn't work anymore; I get a "Thread was being aborted"
    exception. Even though I put it right at the end of my asp.net method,
    there is some javascript that runs after the method returns, and I
    assume that this is what is interfering with Response.End.

    Is there any other way to do this?

    Chris Botha wrote:
    > After Response.BinaryWrite add the statement
    > Response.End()
    >
    > "Cathryn Johns" <> wrote in message
    > news:...
    > > Hi
    > >
    > > I'm trying to download some content as a file to the client. My code
    > > behind looks like this:
    > >
    > > private void DownloadFile(byte[] contents)
    > > {
    > > Response.ContentType = "text/csv";
    > > Response.AppendHeader("content-disposition",
    > > "attachment;filename=myFile.csv");
    > > Response.AppendHeader("content-length",contents.Length.ToString());
    > > Response.BinaryWrite(contents);
    > > }
    > >
    > > It works to an extent, in that it pops up the file download dialog and
    > > writes a file to the user's selected path. But the problem is what's
    > > being written: the first part of the file is the byte array that I'm
    > > trying to write, but the rest is my page's html (see below for sample
    > > output). I thought that setting the content-length header to the
    > > length of the byte array would force it to only write that many bytes,
    > > but it still writes 1.12Kb regardless.
    > >
    > > I just don't know what else to try; any ideas would be appreciated.
    > >
    > > Sample output (the HIPQ is the contents of the byte array):
    > > HIPQ
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    > > <HTML>
    > > <HEAD>
    > > <title>ViewAllocations</title>
    > >
    > > .
    > > .
    > > .
    > > etc.
    > >
    CJ, Nov 2, 2005
    #4
    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. Ian Gordon

    Downloading a file to client

    Ian Gordon, Jul 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    355
    Bill Priess
    Jul 30, 2003
  2. RC
    Replies:
    3
    Views:
    360
    Eliyahu Goldin
    Jan 19, 2005
  3. =?Utf-8?B?VmVua2F0IEthc2FyYW0=?=

    Smart Client Downloading -Sattelite Assembiles

    =?Utf-8?B?VmVua2F0IEthc2FyYW0=?=, Mar 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    367
    =?Utf-8?B?U3RlZmFhbiBWLg==?=
    Mar 10, 2005
  4. Rob Nicholson
    Replies:
    3
    Views:
    466
    Steven Cheng[MSFT]
    May 13, 2005
  5. Replies:
    1
    Views:
    357
    Joerg Jooss
    Dec 3, 2005
Loading...

Share This Page