10

I have some code in a button click event which gets a csv string from a hidden input and writes it to the response as a CSV file.

This work fine in Chrome, Firefox, ie7, ie9 in quirks mode. However it does not work in ie8 or ie9 default.

Looking at this in fiddler the csv is being written to the response but the another get request is being made immediately after and the page reloads. No file saving dialog appears.

    protected void btnCsvHidden_Click(object sender, EventArgs e)
    {
        var csv = csvString.Value;
        var filename = "Reporting";

        Response.Clear();
        Response.ClearHeaders();
        Response.AddHeader("Cache-Control", "no-store, no-cache");
        Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
        Response.ContentType = "text/csv";
        Response.Write(csv);
        Response.End();
    }
JBB
  • 657
  • 5
  • 14
  • 2
    Have you had a look at [this](http://stackoverflow.com/questions/393647/response-content-type-as-csv) related SO question? – Toni Toni Chopper Dec 19 '12 at 17:08
  • Is there a live URL where this can be seen? Are you sure there aren't any other event handlers that are causing the page to refresh before the download prompt is generated? Do you see the same problem if you change the Content-Type to application/octet-stream ? – EricLaw Dec 19 '12 at 17:09
  • Unfortunately I dont have a live example, I'm beginning to think this is a problem with my own ie installation. – JBB Dec 19 '12 at 17:13
  • yes I still have the same problem with different content-types set – JBB Dec 19 '12 at 17:13
  • I copied your code into my own page and ran it in IE9 and was able to download it... Sounds like it's an issue on your end. What's the get request that's being made after the postback? – Dave Zych Dec 19 '12 at 17:46
  • Are you running this code using https? – Fabio Dec 19 '12 at 18:30

1 Answers1

3

The problem was with my own IE, I ran a load of windows updates and now it works so I'm not sure what it was exactly.

JBB
  • 657
  • 5
  • 14