打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
apache下载docx,pptx文件被浏览器当作zip文件保存:修改apache MIM...

apache下载docx,pptx文件被浏览器当作zip文件保存:修改apache MIME类型配置mime.types

apache web服务器的站点,下载pptx,docx,xlsx文件,却被浏览器当作zip文件保存。

这不是浏览器的问题,而是apache不认docx,pptx,,xlsx等Microsoft Office 2007+的文件格式,而这些文件本身是zip压缩文件,所以被apache当作zip压缩文件发给浏览器了。

做个形像一点的比喻:浏览器请求一个url路径,这个路径指向一个.pptx文件,apache从web服务器的目录(磁盘上)查到这个文件,但它不知道这个文件是什么类型(注意:文件类型并不是由文件名后缀决定的,而是文件本身内容所决定),于是它通过分析这个文件内容,发现这个文件像是个zip压缩档,文件大小为12345字节,于是它给浏览器说:“嘿,你请求的是个zip压缩文件,它的大小为12345个字节,文件名是 abcd.pptx 。下面是这个文件的内容,请接收”

浏览器收到消息后,得知apache要发给自己一个zip压缩档,于是一边弹出对话框、询问用户是直接打开、还是保存到硬盘,一边把apache接下来发回的数据保存到缓冲区(或者保存到硬盘的临时目录里),等用户做好选择后,采取相应的打开或是保存的操作。

形象的说明,并不是十分准确的,但大体过程如此。

那现在的问题在于apache因为不认识这个pptx文件,那我们要教会apache,让它知道。我们可以通过mime.types文件让告诉它:

修改mime.types文件,在其中加上

application/vnd.ms-word.document.macroEnabled.12 docmapplication/vnd.openxmlformats-officedocument.wordprocessingml.document docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.template dotxapplication/vnd.ms-powerpoint.template.macroEnabled.12 potmapplication/vnd.openxmlformats-officedocument.presentationml.template potxapplication/vnd.ms-powerpoint.addin.macroEnabled.12 ppamapplication/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsmapplication/vnd.openxmlformats-officedocument.presentationml.slideshow ppsxapplication/vnd.ms-powerpoint.presentation.macroEnabled.12 pptmapplication/vnd.openxmlformats-officedocument.presentationml.presentation pptxapplication/vnd.ms-excel.addin.macroEnabled.12 xlamapplication/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsbapplication/vnd.ms-excel.sheet.macroEnabled.12 xlsmapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsxapplication/vnd.ms-excel.template.macroEnabled.12 xltmapplication/vnd.openxmlformats-officedocument.spreadsheetml.template xltx

这样的一部分代码,这里包含了ms office相关的很多文件类型。

mime.types文件位置

mime.types文件在什么位置呢?

对于windows版本的apache,它在apache安装目录下的conf子目录里,典型位置为 X:\Program Files\Apache Group\Apache2\conf\mime.types

而linux版本下,通常位于/etc/mime.types, 如redhat/centos/fedora都是如此,debian/ubuntu系列应该也在这里,个人没有亲自查证。

不过centos并不推荐在这里修改,参看这里 http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-apache-config.html TypesConfig一小节

Instead of editing /etc/mime.types, the recommended way to add MIME type mappings is to use the AddType directive.

建议不要去修改 /etc/mime.types ,添加MIME type的推荐方法是使用apache的 AddType 指令.

AddType application/vnd.ms-word.document.macroEnabled.12 docmAddType application/vnd.openxmlformats-officedocument.wordprocessingml.document docxAddType application/vnd.openxmlformats-officedocument.wordprocessingml.template dotxAddType application/vnd.ms-powerpoint.template.macroEnabled.12 potmAddType application/vnd.openxmlformats-officedocument.presentationml.template potxAddType application/vnd.ms-powerpoint.addin.macroEnabled.12 ppamAddType application/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsmAddType application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsxAddType application/vnd.ms-powerpoint.presentation.macroEnabled.12 pptmAddType application/vnd.openxmlformats-officedocument.presentationml.presentation pptxAddType application/vnd.ms-excel.addin.macroEnabled.12 xlamAddType application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsbAddType application/vnd.ms-excel.sheet.macroEnabled.12 xlsmAddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsxAddType application/vnd.ms-excel.template.macroEnabled.12 xltmAddType application/vnd.openxmlformats-officedocument.spreadsheetml.template xltx

可以把这些指令加到apache的配置文件里,该指令在apache里出现过N次,不需多说,照着加进去就对了。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MIME类型
office 2007 MIME
寻求*.docx,*.xlsx,*.pptx文件下载后变成.zip文件的解决办法
NodeJs之word文件生成与解析
《selenium2 python 自动化测试实战》(14)——下载文件
Open Office XML 格式里如何描述多段具有不同字体设置的段落
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服