文件导出Excel、Word、Pdf
如果要将查询结果导出Excel,只要将页面的Context-Type修改下:
header( "Content-Type: application/vnd.ms-excel">
如果希望能够提供打开/保存的对话框可以配置Content-Disposition参数,这个参数本来是为了在客户端另存文件时提供一个建议的文件名,但是考虑到安全的原因,就从规范中去掉了。
具体使用如:
response.addHeader("Content-Disposition: inline; filename=文件名.mp3"); --- inline表示在线打开
response.addHeader("Content-Disposition:attachment; filename=test.xls"); --- attachment表示作为附件下载
IE根据Content-Disposition中filename的后缀来识别文件类型,如果有多种文件类型,可以将Content-Type设置为二进制模式:
response.addHeader("Content-type: application/octet-stream");
另外还有一些其他的mimetypes = array(
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'dir' => 'application/x-director',
'js' => 'application/x-javascript',
'swf' => 'application/x-shockwave-flash',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mp3' => 'audio/mpeg',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'wav' => 'audio/x-wav',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'png' => 'image/png',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'txt' => 'text/plain',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
);