网页上用于查看或下载 PDF 的链接

发布于 2025-01-07 10:06:11 字数 190 浏览 0 评论 0 原文

我以前从未考虑过这个问题,但是有没有办法控制用户单击 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云仙小弟 2025-01-14 10:06:11

您可以链接到资产,也可以将数据流式传输到浏览器。这是您仅有的两个选择。

如果链接到文件:它是否在浏览器中打开完全取决于最终用户的浏览器和操作系统首选项。

您可以通过将文件流式传输到浏览器来强制下载文件,这通常会触发浏览器的另存为对话框。

不过,从用户体验的角度来看,最好的选择是简单地链接到 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.

泡沫很甜 2025-01-14 10:06:11

PDF 的显示方式取决于用户的浏览器版本和配置。例如,Chrome 默认包含 PDF 查看器,但用户可以更改插件的行为(自动打开 PDF、禁用、询问用户)。

一种方法是设置 ContentType 和 Content-Disposition,以便浏览器知道如何处理请求。例如,在 ASP.NET 中,您可以这样做:

Response.ContentType = "application/pdf";

Response.AddHeader("Content-Disposition", "attachment;filename=filename.pdf");

披露:我从 这篇文章

如果有帮助,请告诉我。

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:

Response.ContentType = "application/pdf";

Response.AddHeader("Content-Disposition", "attachment;filename=filename.pdf");

Disclosure: I hijacked this code from this article

Let me know if this helps.

不乱于心 2025-01-14 10:06:11

有几种使用服务器端技术的方法:

您可以链接到提供文件下载的 .net / php 页面,例如:

<a href="/ServeFile.aspx?filename=sample.pdf">download pdf</a>

或显示:

<a href="/sample.pdf">view pdf</a>

如果您使用 itextsharp 生成 pdf,您可以将以下内容添加到响应对象强制下载:

Response.AddHeader(
  "Content-Disposition",
  "attachment; filename=itext.pdf"
);

或在同一窗口中打开以下内容:

Response.AddHeader(
 "Content-disposition",
 "inline; filename=itext.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:

<a href="/ServeFile.aspx?filename=sample.pdf">download pdf</a>

or to display:

<a href="/sample.pdf">view pdf</a>

If you are using itextsharp to generate your pdf, you can add the following to the Response object to force a download:

Response.AddHeader(
  "Content-Disposition",
  "attachment; filename=itext.pdf"
);

or the following to open in the same window:

Response.AddHeader(
 "Content-disposition",
 "inline; filename=itext.pdf");   

The user can always set their adobe reader plugin to always download, in which case the browser window display won't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文