网页直接打开PDF或强制下载以及header和content

您所在的位置:网站首页 360浏览器查看pdf文件总是提示下载 网页直接打开PDF或强制下载以及header和content

网页直接打开PDF或强制下载以及header和content

2024-07-18 00:42:41| 来源: 网络整理| 查看: 265

我们经常会看到在网页中点击一个文件,比如pdf的,它有的是直接在网页中打开,有的是提示下载,这两种效果怎么实现的呢?我要写的是在php语言开发的网站中的方法,至于其他的以后再整理:

//php 网页直接打开PDF

header('Content-type: application/pdf');

header('filename='.$fname);                //随便一个名字都可以

readfile($pfname);                               //file的相对地址

//php 强制下载PDF文件

header('Content-type: application/pdf');

header('Content-Disposition: attachment; filename='.$fname);                //随便一个名字都可以

readfile($pfname);                               //file的相对地址

就是以上的这些代码,将其放在php文件的最上面即可,就这么简单

以下是关于header和content-type参数详解,有兴趣的可以继续往下看。

header()是用来发送 HTTP Header的。replace是个可选的参数,指示是否替代一个先期相似的header,或者增加第2个相同类型的header。默认的是替 代,但是如果你传递FALSE参数给第2个参数(即replace),你能强制使用多个相同类型的header。例如:

第 二个可选的参数http_response_code强置HTTP响应代码的值。(PHP>4.3.0)有两种特定的 header调 用。一种是header用字符串”HTTP/”开头的(大小写无所谓),它用来指定发出的HTTP状态码。举个例子来说,如果你已经配置 Apache使用javascript:;" onClick="javascript:tagshow(event, 'PHP');" target="_self">PHP脚 本来处理请求不存在的文件(用ErrorDocument指令),你可能想确认你的脚本生成一个合适的状态码:

注意:HTTP状态行 总是最先发送给客户端的,不管你实际上在什么时候调用header()函数。除非你的HTTP header已经发送出去了,否则,在任何时候,状态将会被header()函数用一个新的状态行 覆盖。

在PHP 3中,只有PHP编译为Apache的模块是,并且用下面Staus Header才能取得同样的效果

第二中特殊的情况就是”Location:”header。如果没有某些 3xx的状态码已经发出的话,它不仅发出Location行,而且发出一个重定向的状态码(302)。

注意:HTTP/1.1要求一个绝对 URI作为Location的参数:包括协议头,主机名和绝对路径,但是某些客户端可以接受相对URI。你可 用$_SERVER['HTTP_HOST'] $_SERVER['PHP_SELF']和dirname()来从一个相对URI来产生绝对 URI。

PHP脚本通常生成一些动态的内容,不必被客户端的浏览器或者代理服务器缓 存。许多代理和客户端可以强制禁止缓存:

注意:你可能发现即使你没有输出上面的header,你的页面还是不能被 缓存。可能是有几个选项改变了默认的缓存行为。见sessrion_cache_limiter().

如果你想提示用户保 存你 发送过去的数据,比如一个已经生成的PDF文件,你可以用Content_Dispostion头来提供一个推荐的文件名,强迫浏览器显示保存对 话框.

对 于IE,要加上Connection头

header(”HTTP/1.1 301 Moved Permanently”);

header(”Location: “.$url);

header(”Connection: close”);

 

header 中Content-type的种类:

'hqx' -> 'application/mac-binhex40', 'cpt' -> 'application/mac-compactpro', 'doc' -> 'application/msword', 'bin' -> 'application/octet-stream', 'dms' -> 'application/octet-stream', 'lha' -> 'application/octet-stream', 'lzh' -> 'application/octet-stream', 'exe' -> 'application/octet-stream', 'class' -> 'application/octet-stream', 'so' -> 'application/octet-stream', 'dll' -> 'application/octet-stream', 'oda' -> 'application/oda', 'pdf' -> 'application/pdf', 'ai' -> 'application/postscript', 'eps' -> 'application/postscript', 'ps' -> 'application/postscript', 'smi' -> 'application/smil', 'smil' -> 'application/smil', 'mif' -> 'application/vnd.mif', 'xls' -> 'application/vnd.ms-excel', 'ppt' -> 'application/vnd.ms-powerpoint', 'wbxml' -> 'application/vnd.wap.wbxml', 'wmlc' -> 'application/vnd.wap.wmlc', 'wmlsc' -> 'application/vnd.wap.wmlscriptc', 'bcpio' -> 'application/x-bcpio', 'vcd' -> 'application/x-cdlink', 'pgn' -> 'application/x-chess-pgn', 'cpio' -> 'application/x-cpio', 'csh' -> 'application/x-csh', 'dcr' -> 'application/x-director', 'dir' -> 'application/x-director', 'dxr' -> 'application/x-director', 'dvi' -> 'application/x-dvi', 'spl' -> 'application/x-futuresplash', 'gtar' -> 'application/x-gtar', 'hdf' -> 'application/x-hdf', 'js' -> 'application/x-javascript', 'skp' -> 'application/x-koan', 'skd' -> 'application/x-koan', 'skt' -> 'application/x-koan', 'skm' -> 'application/x-koan', 'latex' -> 'application/x-latex', 'nc' -> 'application/x-netcdf', 'cdf' -> 'application/x-netcdf', 'sh' -> 'application/x-sh', 'shar' -> 'application/x-shar', 'swf' -> 'application/x-shockwave-flash', 'sit' -> 'application/x-stuffit', 'sv4cpio' -> 'application/x-sv4cpio', 'sv4crc' -> 'application/x-sv4crc', 'tar' -> 'application/x-tar', 'tcl' -> 'application/x-tcl', 'tex' -> 'application/x-tex', 'texinfo' -> 'application/x-texinfo', 'texi' -> 'application/x-texinfo', 't' -> 'application/x-troff', 'tr' -> 'application/x-troff', 'roff' -> 'application/x-troff', 'man' -> 'application/x-troff-man', 'me' -> 'application/x-troff-me', 'ms' -> 'application/x-troff-ms', 'ustar' -> 'application/x-ustar', 'src' -> 'application/x-wais-source', 'xhtml' 'application/xhtml+xml', 'xht' -> 'application/xhtml+xml', 'zip' -> 'application/zip', 'au' -> 'audio/basic', 'snd' -> 'audio/basic', 'mid' -> 'audio/midi', 'midi' -> 'audio/midi', 'kar' -> 'audio/midi', 'mpga' -> 'audio/mpeg', 'mp2' -> 'audio/mpeg', 'mp3' -> 'audio/mpeg', 'aif' -> 'audio/x-aiff', 'aiff' -> 'audio/x-aiff', 'aifc' -> 'audio/x-aiff', 'm3u' -> 'audio/x-mpegurl', 'ram' -> 'audio/x-pn-realaudio', 'rm' -> 'audio/x-pn-realaudio', 'rpm' -> 'audio/x-pn-realaudio-plugin', 'ra' -> 'audio/x-realaudio', 'wav' -> 'audio/x-wav', 'pdb' -> 'chemical/x-pdb', 'xyz' -> 'chemical/x-xyz', 'bmp' -> 'image/bmp', 'gif' -> 'image/gif', 'ief' -> 'image/ief', 'jpeg' -> 'image/jpeg', 'jpg' -> 'image/jpeg', 'jpe' -> 'image/jpeg', 'png' -> 'image/png', 'tiff' -> 'image/tiff', 'tif' -> 'image/tiff', 'djvu' -> 'image/vnd.djvu', 'djv' -> 'image/vnd.djvu', 'wbmp' -> 'image/vnd.wap.wbmp', 'ras' -> 'image/x-cmu-raster', 'pnm' -> 'image/x-portable-anymap', 'pbm' -> 'image/x-portable-bitmap', 'pgm' -> 'image/x-portable-graymap', 'ppm' -> 'image/x-portable-pixmap', 'rgb' -> 'image/x-rgb', 'xbm' -> 'image/x-xbitmap', 'xpm' -> 'image/x-xpixmap', 'xwd' -> 'image/x-xwindowdump', 'igs' -> 'model/iges', 'iges' -> 'model/iges', 'msh' -> 'model/mesh', 'mesh' -> 'model/mesh', 'silo' -> 'model/mesh', 'wrl' -> 'model/vrml', 'vrml' -> 'model/vrml', 'css' -> 'text/css', 'html' -> 'text/html', 'htm' -> 'text/html', 'asc' -> 'text/plain', 'txt' -> 'text/plain', 'rtx' -> 'text/richtext', 'rtf' -> 'text/rtf', 'sgml' -> 'text/sgml', 'sgm' -> 'text/sgml', 'tsv' -> 'text/tab-separated-values', 'wml' -> 'text/vnd.wap.wml', 'wmls' -> 'text/vnd.wap.wmlscript', 'etx' -> 'text/x-setext', 'xsl' -> 'text/xml', 'xml' -> 'text/xml', 'mpeg' -> 'video/mpeg', 'mpg' -> 'video/mpeg', 'mpe' -> 'video/mpeg', 'qt' -> 'video/quicktime', 'mov' -> 'video/quicktime', 'mxu' -> 'video/vnd.mpegurl', 'avi' -> 'video/x-msvideo', 'movie' -> 'video/x-sgi-movie', 'ice' -> 'x-conference/x-cooltalk'


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭