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

后端 未结 12 509
梦如初夏
梦如初夏 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:45

    We recently ran into this problem ourselves. See this MSKB article

    These are the headers we ended up having to use to get it to work over SSL.

    header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$file_name\";");
    header("Content-length: " . strlen($csv_string));
    
    0 讨论(0)
  • 2020-12-02 10:47

    Some time ago I've got a problem with IE6 opening pdf files, and crashing when AdobeReader 6.0 was installed and tried to open file in browser window. Than I found somewhere this header:

    header('Content-Type: application/force-download');
    

    And it solved the problem, every pdf file was downloaded and opened in Adobe instead of IE.

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

    The solution for me was:

    header_remove(); 
    header('Content-Type: text/csv'); 
    header('Content-Disposition: attachment; filename=brokerlist.csv'); 
    echo $content;
    
    0 讨论(0)
  • 2020-12-02 10:50

    The only extra code I had to add for IE to work with SSL was: header("Pragma: public"); So my headers look like this now:

     header("Pragma: public");  
     header("Content-Type: application/octet-stream");
     header("Content-Disposition: attachment; filename=some_filename.csv");
    
    0 讨论(0)
  • 2020-12-02 10:52

    Don't we love IE? :)

    Try using those headers:

      header("Pragma: public");
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: private",false);
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename=\"exportevent.csv\";" );
      header("Content-Transfer-Encoding: binary"); 
    

    I think that the octet-stream content type forces IE to download the file.

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

    We have just had the same issue and after adding many headers and getting a working link I then removed them one by one and found the key one for us was "Cache-Control: public" so in the end we just had

    header("Cache-Control: public"); 
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=some_filename.csv");
    

    which worked fine.

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