13
2025
03
19:41:04

浏览器打开pdf格式文件自动预览

  有一朋友需打开Web服务器(nginx)中pdf格式文件实现自动预览--缺省为下载文件,于是网上查阅了些资料,发现是一个名为Content-Disposition的HTTP头部在控制:

nginx 服务器

Nginx服务器为例:

    server {
        listen       8080;
        server_name  _;
        root         /usr/share/nginx/html;

        location ~* \.(pdf|txt|log)$ {
            add_header Content-Disposition inline;
            #add_header Content-Disposition attachment;
           }
        }
  • inline: 自动预览动作

  • attachment: 下载动作

Apache 服务器

<FilesMatch "\.(?i:pdf)$">
      ForceType application/octet-stream
      Header set Content-Disposition attachment
</FilesMatch>

预览

经过测试 Chrome、Firefox 皆正常

02

注意要点

  • 文件名尽量不使用中文

参考引用




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://sg.hqyman.cn/post/9545.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: