资源作为文档解释,但使用 MIME 类型 application/csv 进行转移

我有一个按键可以下载 CSV 文件。 文件似乎下载得很好,但是我会收到以下的警告。 这个警告似乎只会在 Chrome 中出现。

> 资源作为文档解释,但使用 MIME 类型 application/csv 进行转移:

这就是我设置 CSV 头的地方。

/**
* 设置 PHP MIME 头
*
* 这将覆盖先前的任何标头
*
* @param string $header 需要设置的任何自定义标头
*
* @return void
* @access public
*/
function setHeader($header='')
{
    if ($header) {
        //设置自定义标头
        header($header);
    } else {
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename="'.$this->filename.'";');

        //已知 Internet Explorer 在 SSL 连接 (https) 方面存在问题。
        //http://support.microsoft.com/default.aspx?scid=kb;en-us;812935
        //http://support.microsoft.com/default.aspx?scid=kb;en-us;316431
        //在 https 中不要发送不缓存头给 IE

        //获取协议信息
        $url_info = parse_url(current_url());
        $protocol = (isset($url_info['scheme'])) ? $url_info['scheme'] : NULL;
        $using_ie6_8 = isset ($_SERVER['HTTP_USER_AGENT']) ? preg_match('/(MSIE\s[0-8]+\.)/', $_SERVER['HTTP_USER_AGENT']) : FALSE;
        if (!is_null($protocol) && $protocol == 'https' && $using_ie6_8) {
            header('Cache-Control: private, max-age=0, must-revalidate');
            header('Pragma: private');
            header('Expires: '.gmdate('D, d M Y H:i:s', time()-3600).' GMT');

        }

    }

}//结束 setHeader()

响应头

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Disposition:attachment; filename="submission_log.csv";
Content-Type:application/csv
Date:Thu, 22 Sep 2016 04:59:06 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Pragma:no-cache
Server:openresty
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN

请求头

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:6252
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary4CxCF8KLzdtoGRO2
Cookie:lastSelectedLinkId=2661; lastSelectedAssetId=2151; SQ_SYSTEM_SESSION=o44hori3h2fcu5ltm29u6rv2k44bq1tu6v3364pdi8jn273scebnjm3lsuik5teasneeh664j98o3ajbur6p39kque68qgjohkktsk3; _ga=GA1.2.545099348.1472451817
Host:matrixdev.squiz.net
Origin:http://matrixdev.squiz.net
Referer:http://matrixdev.squiz.net/_admin/?SQ_BACKEND_PAGE=main&backend_section=am&am_section=edit_asset&assetid=2151&sq_asset_path=70%2C2151&sq_link_path=71%2C2661&asset_ei_screen=log
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
点赞
用户7117394
用户7117394

如果你在<a>标签中放置了下载 CSV 文件的链接,需要在<a>中添加“download”属性。

2016-12-08 20:39:49