How to use the CSV MIME-type?

前端 未结 4 748
感情败类
感情败类 2020-11-27 03:11

In a web application I am working on, the user can click on a link to a CSV file. There is no header set for the mime-type, so the browser just renders it as text. I would

相关标签:
4条回答
  • 2020-11-27 03:30

    You are not specifying a language or framework, but the following header is used for file downloads:

    "Content-Disposition: attachment; filename=abc.csv"
    
    0 讨论(0)
  • 2020-11-27 03:36

    This code can be used to export any file, including csv

    // application/octet-stream tells the browser not to try to interpret the file
    header('Content-type: application/octet-stream');
    header('Content-Length: ' . filesize($data));
    header('Content-Disposition: attachment; filename="export.csv"');
    
    0 讨论(0)
  • 2020-11-27 03:37

    With Internet Explorer you often have to specify the Pragma: public header as well for the download to function properly..

    header('Pragma: public');
    

    Just my 2 cents..

    0 讨论(0)
  • 2020-11-27 03:49

    You could try to force the browser to open a "Save As..." dialog by doing something like:

    header('Content-type: text/csv');
    header('Content-disposition: attachment;filename=MyVerySpecial.csv');
    echo "cell 1, cell 2";
    

    Which should work across most major browsers.

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