做下载功能的时候,经常需要定义header头,但是header可以定义的属性太多了,有时候会显得无从下手。这里记录一下我用到的几个header头,并且给出它们的作用以及参考链接。
header("Content-Type: application/force-download");
参考链接: //http://www.thinkphp.cn/topic/36582.html
功能: 告诉浏览器强制下载
header("Content-type:application/octet-stream");
参考链接 //https://blog.youkuaiyun.com/wangjun5159/article/details/49644507
功能: 二进制流
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
参考链接: //https://blog.youkuaiyun.com/xiaoranzhizhu/article/details/70473734
功能: office所有后缀对应的 content-type
header("Content-Transfer-Encoding: binary");
参考链接: //https://blog.youkuaiyun.com/foolish0421/article/details/73302336
header("Accept-Ranges:bytes");
参考链接: //https://blog.youkuaiyun.com/runje/article/details/47720641
header("Content-Disposition:attachment;filename=" . $filename . ".csv");
参考链接://https://blog.youkuaiyun.com/ssssny/article/details/77717287
功能: 表明不在页面输出打开,直接下载
header("Pragma: no-cache"); //无缓存
header("Expires: 0");
参考链接: //https://www.jianshu.com/p/f331d5f0b979
功能: 该资源过期的日期,浏览器会根据该过期日期与客户端时间对比,如果过期时间还没到,则会去缓存中读取该资源,如果已经到期了,则浏览器判断为该资源已经不新鲜要重新从服务端获取
此外还有很多的header属性,这里就不一一介绍了,网上有很多,一查一大堆,大家根据需要再去查看即可。
注意: 如果是要下载的话,十有八九要定义header的,如果是类似于直接写入到csv文件,那么就不需要用到header,一切根据需求决定。
end