How do I use Visualforce to generate a CSV file that can be downloaded using IE8?

I am using the following Visualforce Page as an endpoint that generates a CSV file using POST data that it is handed. The following page definition works great for everything but IE8 (Internet Explorer 8):

<apex:page contentType="text/csv#{!fileName}.csv" 
        showHeader="false" sidebar="false" standardStylesheets="false">
     <apex:outputText value="{!input}" escape="false"/>
</apex:page>

with controller

public with sharing class ExportController {

// Controller for ExportData.page,
// which expects to be handed the following POST variables:
// @inputdata(string) data to export (i.e. in CSV format)
// @filename(string) name of the file to export, e.g. 'AccountData' 

public transient String input { public get; private set; }
public transient String fileName { public get; private set; }

public ExportController() {
    Map<String,String> params = ApexPages.currentPage().getParameters();

    // We expect to be handed POST variables called 'inputdata' and 'filename'
    fileName = params.get('filename');
    if (fileName == null) fileName = 'Data';

    input = params.get('inputdata');
    if (input == null) input = 'No data provided.';
}
}

In IE8, the error I get is:

Internet Explorer cannot download ExportData from
nexus.na12.visual.force.com. Interent Explorer was not able to open
this Internet site. The requested site is either unavailable or cannot
be found. Please try again later.

I know that the page is not unavailable, however, because I am getting debug logs from it using Developer Console. So it must be an IE8 file type handling issue of some sort.

Answer

You have to set cache="true" and contentType="application/octet-stream
instead of text/csv.

<apex:page cache="true" 
        contentType="application/octet-stream#{!fileName}.csv" 
        showHeader="false" sidebar="false" standardStylesheets="false">
     <apex:outputText value="{!input}" escape="false"/>
</apex:page>

The key pieces here are cache=true, which, in conjunction with the default expires=0 attribute, produces the following HTTP headers:

"Cache-Control: must-revalidate, post-check=0, pre-check=0"
"Cache-Control: public"

And then the contentType of application/octet-stream — which produces the following HTTP header:

"Content-Type: application/octet-stream"

See this article for additional explanation of IE8’s handling of these headers.

Attribution
Source : Link , Question Author : zachelrath , Answer Author : Community

Leave a Comment