How do I get csv file to download on IE? Works on firefox

后端 未结 12 508
梦如初夏
梦如初夏 2020-12-02 10:10

I\'m struggling with an odd error. I have a simple web app that grabs stuff from a DB then outputs it as a downloadable csv file. It works on firefox and chrome, but IE fail

相关标签:
12条回答
  • 2020-12-02 10:26

    I've had success with the following:

    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: attachment; filename=File.csv");
    

    Setting the type to application/vnd.ms-excel seemed to do the trick in my case. This is all in a file that is opened by submitting a form using

    target="_blank"
    
    0 讨论(0)
  • 2020-12-02 10:27

    Try setting your content type to text/csv instead of application/octet-stream.

    Since application/octet-stream is a generic binary mime type (and doesn't match the '.csv' extension), Internet explorer might be ignoring it and computing the mime type based on the file extension.

    0 讨论(0)
  • 2020-12-02 10:28

    Did you try the Content-type: text/csv ?

    0 讨论(0)
  • 2020-12-02 10:31

    If you are trying to accomplish this task (getting a CSV file to download in IE8) using Salesforce.com (in which case your front-end is Visualforce and you can't set all of the headers, only some of them), here's what you need:

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

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

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    

    And then the contentType of application/octet-stream --- doing text/csv fails for IE8.

    0 讨论(0)
  • 2020-12-02 10:35

    After using Javascript it will solve your problem.

    Use this for IE,

        var IEwindow = window.open();
        IEwindow.document.write('sep=,\r\n' + CSV);
        IEwindow.document.close();
        IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
        IEwindow.close();
    

    For more information i have written tutorial on that, see - Download JSON data in CSV format Cross Browser Support

    Hope this will be helpful for you.

    0 讨论(0)
  • 2020-12-02 10:38

    This simply doesn't make sense. I tried the accepted answer, all the other answers in here, and it didn't work for me. I tried their permutations, and somehow I managed to make it work in IE like so:

     header("Pragma: public");
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
     header("Cache-Control: public");
     header("Content-Type: application/vnd.ms-exce");
     header("Content-Disposition: attachment; filename=coupons.csv" );
     header("Content-Transfer-Encoding: binary");       
     header("Content-Length: " . strlen($csv)); 
     echo $csv;
     die();
    

    One thing I did is to empty the cache every freaking time I test the code. And it still doesn't make sense. Just in case someone might need this desperately ;)

    0 讨论(0)
提交回复
热议问题