如何强制文件在浏览器中打开而不是下载(PDF)? [英] How do I force files to open in the browser instead of downloading (PDF)?

查看:622
本文介绍了如何强制文件在浏览器中打开而不是下载(PDF)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当取消选中在浏览器中显示PDF选项时,是否有办法强制PDF文件在浏览器中打开?

Is there a way to force PDF files to open in the browser when the option "Display PDF in browser" is unchecked?

我尝试使用embed标签和iframe,但只有在选中该选项时才有效。

I tried using the embed tag and an iframe, but it only works when that option is checked.

我该怎么办?

推荐答案

要向浏览器表明应该在浏览器中查看该文件:

To indicate to the browser that the file should be viewed in the browser:

Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"

要下载文件而不是查看文件:

To have the file downloaded rather than viewed:

Content-Type: application/pdf
Content-Disposition: attachment; filename="filename.pdf"

如果文件名包含特殊字符,则需要文件名周围的引号as filename [1] .pdf 这可能会破坏浏览器处理响应的能力。

The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

这篇关于如何强制文件在浏览器中打开而不是下载(PDF)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆