CSV File Download from an ASP.NET page issue

Discussion in 'ASP .Net' started by =?Utf-8?B?QmVybmFyZCBNYXR0aGV3cw==?=, Dec 17, 2004.

  1. I'm using the following code to stream a CSV file download to the client
    browser -

    HttpContext.Current.Response.Buffer = true;
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    HttpContext.Current.Response.AddHeader( "content-disposition","attachment;
    filename=" + filename );
    HttpContext.Current.Response.BinaryWrite(bytes);
    HttpContext.Current.Response.End();

    (where <filename> is equal to the filename, eg test.csv; and <bytes> is
    equal to a bytearray of the actual CSV data)

    Everything is fine and dandy except for one tiny glitch - The "File
    Download" dialog box (the one that asks Save, Open, Cancel, More Info)
    appears twice in succession when you click on Open. If you click on Save,
    the CSV file is saved ok to the clients machine then the user is prompted to
    Open the file. If you simply click on Open, the same dialog box immidiately
    reappers and you have to click on Open again to display the file in Excel.
     
    =?Utf-8?B?QmVybmFyZCBNYXR0aGV3cw==?=, Dec 17, 2004
    #1
    1. Advertising

  2. I've just been trying to solve the same problem when downloading text files
    to the client. However, after a couple of hours scouring the net, the
    following URL discusses the same problem which appears to be a bug in certain
    IE browsers.

    Apparently 'longhorn' fixes this.

    The article discusses the fact that this works if the method used is GET as
    opposed to POST. I have still not found any documentation from Microsoft
    detailing this problem - can anyone else enlighten us?

    http://www.dotnet247.com/247reference/msgs/48/243943.aspx

    Thanks, Claire-Louise

    "Bernard Matthews" wrote:

    > I'm using the following code to stream a CSV file download to the client
    > browser -
    >
    > HttpContext.Current.Response.Buffer = true;
    > HttpContext.Current.Response.ClearContent();
    > HttpContext.Current.Response.ClearHeaders();
    > HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    > HttpContext.Current.Response.AddHeader( "content-disposition","attachment;
    > filename=" + filename );
    > HttpContext.Current.Response.BinaryWrite(bytes);
    > HttpContext.Current.Response.End();
    >
    > (where <filename> is equal to the filename, eg test.csv; and <bytes> is
    > equal to a bytearray of the actual CSV data)
    >
    > Everything is fine and dandy except for one tiny glitch - The "File
    > Download" dialog box (the one that asks Save, Open, Cancel, More Info)
    > appears twice in succession when you click on Open. If you click on Save,
    > the CSV file is saved ok to the clients machine then the user is prompted to
    > Open the file. If you simply click on Open, the same dialog box immidiately
    > reappers and you have to click on Open again to display the file in Excel.
     
    =?Utf-8?B?Q2xhaXJlLUxvdWlzZQ==?=, Dec 22, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?QmVybmFyZCBNYXR0aGV3cw==?=

    workabyte

    Joined:
    May 26, 2012
    Messages:
    1
    This is what works for me, i know this is an old post but i found it, someone else will too.

    Response.Buffer = true;
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "text/csv";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + CSVFile);
    Response.TransmitFile(data.ToCSV(CSVFile));
    Response.End();
    System.IO.File.Delete(CSVFile);
     
    workabyte, May 26, 2012
    #3
    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. M. D'Costa

    Issue saving csv file from asp page

    M. D'Costa, Sep 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    678
    Hermit Dave
    Sep 28, 2004
  2. Ryan Taylor
    Replies:
    2
    Views:
    1,025
    Ryan Taylor
    Nov 10, 2004
  3. Steve C. Orr [MVP, MCSD]
    Replies:
    0
    Views:
    1,667
    Steve C. Orr [MVP, MCSD]
    Mar 7, 2005
  4. Brett  Kelly
    Replies:
    1
    Views:
    724
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  5. Tintin92
    Replies:
    1
    Views:
    1,806
    Andrew Thompson
    Feb 14, 2007
Loading...

Share This Page