VPS参考测评推荐
专注分享VPS主机优惠信息
衡天云优惠活动
华纳云优惠活动
荫云优惠活动

Content-disposition

主机参考: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));

--------------------------------------------------------------
主机参考,收集国内外VPSVPS测评主机测评云服务器虚拟主机独立服务器,国内外服务器高性价比建站主机相关优惠信息@zhujicankao.com
详细介绍和测评国外VPS主机,云服务器,国外服务器,国外主机的相关优惠信息,商家背景,网络带宽等等,也是目前国内最好的主机云服务器VPS参考测评资讯优惠信息分享平台

这几篇文章你可能也喜欢:

本文由主机参考刊发,转载请注明:Content-disposition https://zhujicankao.com/12439.html

【腾讯云】领8888元采购礼包,抢爆款云服务器 每月 9元起,个人开发者加享折上折!
打赏
转载请注明原文链接:主机参考 » Content-disposition
主机参考仅做资料收集,不对商家任何信息及交易做信用担保,购买前请注意风险,有交易纠纷请自行解决!请查阅:特别声明

相关推荐

评论 抢沙发

评论前必须登录!