强制打开“另存为...”弹出窗口,在文本链接处打开,单击 HTML 中的 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3802510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:40:11  来源:igfitidea点击:

Force to open "Save As..." popup open at text link click for PDF in HTML

htmlpdfdownloadmeta-tagscontent-disposition

提问by designer-trying-coding

I have some big size PDF catalogs at my website, and I need to link these as download. When I googled, I found such a thing noted below. It should open the "Save As..." popup at link click...

我的网站上有一些大尺寸的 PDF 目录,我需要将它们作为下载链接。当我用谷歌搜索时,我发现了下面提到的这样一个事情。它应该在链接单击时打开“另存为...”弹出窗口...

 <head>
    <meta name="content-disposition" content="inline; filename=filename.pdf">
    ...

But it doesn't work :/ When I link to a file as below, it just links to file and is trying to open the file.

但它不起作用:/当我链接到如下文件时,它只是链接到文件并试图打开该文件。

    <a href="filename.pdf" title="Filie Name">File name</a>


UPDATE (according to answers below):

更新(根据以下答案):

As I see there is no 100% reliable cross-browser solution for this. Probably the best way is using one of the web services listed below, and giving a download link...

正如我所见,没有 100% 可靠的跨浏览器解决方案。可能最好的方法是使用下面列出的网络服务之一,并提供下载链接...

回答by Ayush Gupta

From an answer to Force a browser to save file as after clicking link:

从对Force a browser to save file as after click link的回答:

<a href="path/to/file" download>Click here to download</a>

回答by DrWaky

<a href="file link" download target="_blank">Click here to download</a>

It works for me in Firefox and Chrome.

它在 Firefox 和 Chrome 中对我有用。

回答by Karel Petranek

Meta tags are not a reliable way to achieve this result. Generally you shouldn't even do this - it should be left up to the user/user agent to decide what do to with the content you provide. The user can always force their browser to download the file if they wish to.

元标记不是实现此结果的可靠方法。通常,您甚至不应该这样做 - 应该由用户/用户代理决定如何处理您提供的内容。如果他们愿意,用户总是可以强制他们的浏览器下载文件。

If you still want to force the browser to download the file, modify the HTTP headers directly. Here's a PHP code example:

如果还想强制浏览器下载文件,直接修改HTTP头即可。这是一个 PHP 代码示例:

$path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');  // Allow support for download resume
header('Content-Length: ' . filesize($path));  // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');  // Change the mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
readfile($path);  // This is necessary in order to get it to actually download the file, otherwise it will be 0Kb

Note that this is just an extension to the HTTP protocol; some browsers might ignore it anyway.

请注意,这只是 HTTP 协议的扩展;无论如何,某些浏览器可能会忽略它。

回答by Tony

I had this same issue and found a solution that has worked great so far. You put the following code in your .htaccessfile:

我遇到了同样的问题,并找到了一个迄今为止效果很好的解决方案。您将以下代码放入您的.htaccess文件中:

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

It came from Force a File to Download Instead of Showing Up in the Browser.

它来自Force a File to Download 而不是出现在浏览器中

回答by User

I found a very simple solution for Firefox (only works with a relative rather than a direct href): add type="application/octet-stream":

我为 Firefox 找到了一个非常简单的解决方案(仅适用于相对而不是直接 href):添加type="application/octet-stream"

<a href="./file.pdf" id='example' type="application/octet-stream">Example</a>

回答by Sharad Gautam

Try adding this line to your .htaccess file.

尝试将此行添加到您的 .htaccess 文件中。

AddType application/octet-stream .pdf

I hope it'll work as it is browser independent.

我希望它能工作,因为它是独立于浏览器的。

回答by Ashay

Generally it happens, because some browsers settings or plug-ins directly open PDF in the same window like a simple web page.

一般会出现这种情况,因为有些浏览器设置或者插件直接在同一个窗口中打开PDF,就像一个简单的网页。

The following might help you. I have done it in PHP a few years back. But currently I'm not working on that platform.

以下内容可能对您有所帮助。几年前我用 PHP 完成了它。但目前我不在那个平台上工作。

<?php
    if (isset($_GET['file'])) {
        $file = $_GET['file'];
        if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
            header('Content-type: application/pdf');
            header("Content-Disposition: attachment; filename=\"$file\"");
            readfile($file);
        }
    }
    else {
        header("HTTP/1.0 404 Not Found");
        echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
    }
?>

Save the above as download.php.

将以上内容另存为download.php

Save this little snippet as a PHP file somewhere on your server and you can use it to make a file download in the browser, rather than display directly. If you want to serve files other than PDF, remove or edit line 5.

将此小片段保存为服务器上某个位置的 PHP 文件,您可以使用它在浏览器中下载文件,而不是直接显示。如果您想提供 PDF 以外的文件,请删除或编辑第 5 行。

You can use it like so:

你可以像这样使用它:

Add the following link to your HTML file.

将以下链接添加到您的 HTML 文件。

<a href="download.php?file=my_pdf_file.pdf">Download the cool PDF.</a>

Reference from: This blog

参考资料:本博客

回答by manish1706

Just put the below code in your .htaccessfile:

只需将以下代码放入您的.htaccess文件中:

AddType application/octet-stream .csv
AddType application/octet-stream .xls
AddType application/octet-stream .doc
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf

Or you can also do trick by JavaScript

或者你也可以通过 JavaScript 来做技巧

element.setAttribute( 'download', whatever_string_you_want);

回答by NowLiveLove

I just used this, but I don't know if it works across all browsers.

我刚用过这个,但我不知道它是否适用于所有浏览器。

It works in Firefox:

它适用于 Firefox:

<a href="myfile.pdf" download>Click to Download</a>

回答by Mr. Bungle

A reallysimple way to achieve this, without using external download sites or modifying headers etc. is to simply create a ZIP file with the PDF inside and link directly to the ZIP file. This will ALWAYS trigger the Save/Open dialog, and it's still easy for people to double-click the PDF windows the program associated with .zip is launched.

实现此目的的一种非常简单的方法,无需使用外部下载站点或修改标题等,只需创建一个包含 PDF 的 ZIP 文件,然后直接链接到 ZIP 文件。这将始终触发“保存/打开”对话框,并且人们仍然可以轻松地双击与 .zip 关联的程序启动的 PDF 窗口。

BTW great question, I was looking for an answer as well, since most browser-embedded PDF plugins take sooo long to display anything (and will often hang the browser whilst the PDF is loading).

顺便说一句,好问题,我也在寻找答案,因为大多数浏览器嵌入的 PDF 插件需要很长时间才能显示任何内容(并且通常会在加载 PDF 时挂起浏览器)。