主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情! |
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作 |
Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。当 Internet Explorer 接收到头时,它会激活文件下载对话框,它的文件名框自动填充了头中指定的文件名。(请注意,这是设计导致的;无法使用此功能将文档保存到用户的计算机上,而不向用户询问保存位置。)
服务端向客户端游览器发送文件时,如果是浏览器支持的文件类型,一般会默认使用浏览器打开,比如txt、jpg等,会直接在浏览器中显示,如果需要提示用户保存,就要利用Content-Disposition进行一下处理,关键在于一定要加上p_w_upload:
Response.AppendHeader("Content-Disposition","p_w_upload;filename=FileName.txt");
备注:这样浏览器会提示保存还是打开,即使选择打开,也会使用相关联的程序比如记事本打开,而不是IE直接打开了。
Content-Disposition就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名。具体的定义如下:
content-disposition = “Content-Disposition” “:”
disposition-type *( “;” disposition-parm )
disposition-type = “p_w_upload” | disp-extension-token
disposition-parm = filename-parm | disp-extension-parm
filename-parm = “filename” “=” ed-string
disp-extension-token = token
disp-extension-parm = token “=” ( token | ed-string )
那么由上可知具体的例子:
Content-Disposition: p_w_upload; filename=“filename.xls”
当然filename参数可以包含路径信息,但User-Agnet会忽略掉这些信息,只会把路径信息的最后一部分做为文件名。当你在响应类型为application/octet- stream情况下使用了这个头信息的话,那就意味着你不想直接显示内容,而是弹出一个”文件下载”的对话框,接下来就是由你来决定“打开”还是“保存” 了。
注意事项:
1.当代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。 response.addHeader("Content-Disposition","p_w_upload");一定要确保没有做过关于禁止浏览器缓存的操作。如下:
response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0);
不然会发现下载功能在opera和firefox里面好好的没问题,在IE下面就是不行,就是找不到文件。
在IE5和Opera中浏览器会不管Content-Disposition头的内容,要再次声明Content-Type,声明Content-Length可以显示下载进度条
代码如下:
$fileName = 'config.ini';
$mimeType = 'application/zip';
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
$mimeType = 'application/x-download';
}
header('Content-Disposition: p_w_upload; filename=' . $fileName);
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . filesize($fileName));
Content disposition is an extension of the MIME Protocol, which instructs the mime user agent how to display additional files. When Internet Explorer receives the header, it activates the file download dialog box, and its filename box automatically fills in the filename specified in the header. (note that this is due to the design; you cannot use this feature to save documents to the user's computer without asking the user for the save location.)
When the server sends files to the client browser, if the file type is supported by the browser, it will be opened by the browser by default, such as TXT, JPG, etc., and will be displayed directly in the browser. If the user needs to be prompted to save, it is necessary to use Content-Disposition for processing. The key is to add p w upload:
Response.AppendHeader("Content-Disposition","p_w_upload;filename=FileName.txt");
Note: in this way, the browser will prompt whether to save or open. Even if you choose to open it, you will also use the associated program such as Notepad to open it instead of ie directly.
Content disposition is to provide a default file name when the user wants to save the requested content as a file. The specific definitions are as follows:
content-disposition = “Content-Disposition” “:”
disposition-type *( “;” disposition-parm )
disposition-type = “p_w_upload” | disp-extension-token
disposition-parm = filename-parm | disp-extension-parm
filename-parm = “filename” “=” ed-string
disp-extension-token = token
disp-extension-parm = token “=” ( token | ed-string )
Then we can see specific exles from the above:
Content-Disposition: p_w_upload; filename=“filename.xls”
Of course, the filename parameter can contain path information, but user agnet will ignore this information and only use the last part of the path information as the filename. When you use this header when the response type is application / octet stream, it means that you don't want to display the content directly, but pop up a "file download" dialog box, and then you decide whether to "open" or "save".
matters needing attention:
1. When content disposition is used in the code to ensure that the download dialog box pops up. Response.addheader ("content disposition", "P" Upload "); make sure that no action has been taken to disable browser caching. As follows:
response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0);
Otherwise, it will be found that the download function is OK in opera and Firefox. It's not OK under ie, I just can't find the file.
In IE5 and opera, the browser will ignore the content of the content disposition header. To declare the content type again, declaring the content length can display the download progress bar
The code is as follows:
$fileName = 'config.ini';
$mimeType = 'application/zip';
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
$mimeType = 'application/x-download';
}
header('Content-Disposition: p_w_upload; filename=' . $fileName);
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . filesize($fileName));
--------------------------------------------------------------
主机参考,收集国内外VPS,VPS测评,主机测评,云服务器,虚拟主机,独立服务器,国内外服务器,高性价比建站主机相关优惠信息@zhujicankao.com
详细介绍和测评国外VPS主机,云服务器,国外服务器,国外主机的相关优惠信息,商家背景,网络带宽等等,也是目前国内最好的主机云服务器VPS参考测评资讯优惠信息分享平台
这几篇文章你可能也喜欢:
- PacificRack,黑色星期五特别优惠,美国便宜VPS云服务器低至1折,美国洛杉矶QN自营机房,KVM虚拟架构,2核4G内存100Mbps带宽,仅15美元/年,约1.25美元/月
- ZJI,双十二特价优惠活动,香港免备案独立服务器5.5折,阿里云CN2专线,双线双IP,E5-2630L(6核12线程)处理器16G内存5Mbps CN2+BGP带宽,412.5元/月
- #实时更新#最新2021年主机商双十一活动汇总,国内外VPS主机商双11促销优惠活动汇总,海外免备案VPS云服务器/独立服务器超大带宽不限流量双11活动
- ZJI,双十一特价优惠,自营免备案香港独立服务器/独服一律55折,香港阿里云专线/BGP,Intel E5-2630L处理器16G内存5Mbps带宽CN2+BGP阿里云专线,412元/月
- ZJI,香港葵湾独立服务器/物理服务器特价优惠,终身55折循环优惠,CN2+BGP线路,E5-2630L处理器16G内存15Mbps带宽,522.5元/月
本文由主机参考刊发,转载请注明:Content-disposition https://zhujicankao.com/12446.html
评论前必须登录!
注册