我以前从未考虑过这个问题,但是有没有办法控制用户单击 PDF 文件链接时发生的情况?
我的老板想提供两个链接来执行以下操作:
1.在浏览器中查看此PDF
2. 下载 PDF
有没有办法做到这一点?我不考虑这些事情,大多数现代浏览器都会在浏览器中打开 PDF。如果我想下载它,我右键单击下载。有什么办法强制行动吗?
谢谢
I have never thought about this before, but is there a way to control what happens when a user clicks a link to a PDF file?
My boss would like to offer two links to do the following:
1. View this PDF in the browser
2. Download the PDF
Is there a way to do this ? I don't think about these kinds of things, most modern browsers will open a PDF in the browser. If I want to download it, I right-click download. Any way to force the action ?
Thanks
发布评论
评论(3)
您可以链接到资产,也可以将数据流式传输到浏览器。这是您仅有的两个选择。
如果链接到文件:
它是否在浏览器中打开完全取决于最终用户的浏览器和操作系统首选项。
您可以通过将文件流式传输到浏览器来强制下载文件,这通常会触发浏览器的
另存为
对话框。不过,从用户体验的角度来看,最好的选择是简单地链接到 PDF,并让用户知道他们将要单击 PDF 链接……这样他们就可以决定如何处理它。
You can link to an asset, or you can stream the data to the browser. Those are the only two options you have on your end.
If you link to a file:
<a href="file.pdf">
whether or not it opens in the browser or not is entirely dependent on the end user's browser and operating system preferences.You can force the download of a file, however by streaming it to the browser, which will usually trigger the browser's
save as
dialog.Your best bet, though, from a user experience perspective, is to simply link to the PDF and let the user know that they are about to click on a PDF link...that way they can decide what they do with it.
PDF 的显示方式取决于用户的浏览器版本和配置。例如,Chrome 默认包含 PDF 查看器,但用户可以更改插件的行为(自动打开 PDF、禁用、询问用户)。
一种方法是设置 ContentType 和 Content-Disposition,以便浏览器知道如何处理请求。例如,在 ASP.NET 中,您可以这样做:
披露:我从 这篇文章
如果有帮助,请告诉我。
How PDFs are displayed are based on the user's browser version and configuration. For example Chrome includes a PDF viewer by default, but the user has the ability to change the behavior of the plug-in ( automatically open PDFs, disable, ask the user).
One way to do this is to set the ContentType and Content-Disposition so the browser will know how to handle the request. For example in ASP.NET you would do it like this:
Disclosure: I hijacked this code from this article
Let me know if this helps.
有几种使用服务器端技术的方法:
您可以链接到提供文件下载的 .net / php 页面,例如:
或显示:
如果您使用 itextsharp 生成 pdf,您可以将以下内容添加到响应对象强制下载:
或在同一窗口中打开以下内容:
用户始终可以将其 adobe reader 插件设置为始终下载,在这种情况下,浏览器窗口显示将无法工作。
There are a few ways using server side technologies:
you can link to a .net / php page that serves the file to download, eg:
or to display:
If you are using itextsharp to generate your pdf, you can add the following to the Response object to force a download:
or the following to open in the same window:
The user can always set their adobe reader plugin to always download, in which case the browser window display won't work.