我有一些大尺寸的PDF目录在我的网站上,我需要链接这些下载。当我在谷歌上搜索时,我发现下面有这样一件事。它应该打开“另存为…”弹出链接点击…

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

但它不工作:/当我链接到一个文件如下,它只是链接到文件,并试图打开文件。

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

更新(根据以下答案):

据我所知,没有100%可靠的跨浏览器解决方案。也许最好的方法是使用下面列出的web服务之一,并提供下载链接…

http://box.net/ http://droplr.com/ http://getcloudapp.com/


当前回答

这是旧的帖子,但这是一个我的解决方案在JavaScript使用jQuery库。

<script>
(function($){
    var download = [];
    $('a.force-download, .force-download a').each(function(){
        // Collect info
        var $this = $(this),
            $href = $this.attr('href'),
            $split = $href.split('/'),
            $name = document.title.replace(/[\W_]/gi, '-').replace(/-{2,}/g, '-'); // get title and clean it for the URL

        // Get filename from URL
        if($split[($split.length-1)])
        {
            $tmp = $split[($split.length-1)];
            $tmp = $tmp.split('.');
            $name = $tmp[0].replace(/[\W_]/gi, '-').replace(/-{2,}/g, '-');
        }

        // If name already exists, put timestamp there
        if($.inArray($name, download) > -1)
        {
            $name = $name + '-' + Date.now().replace(/[\W]/gi, '-');
        }

        $(this).attr("download", $name);
        download.push($name);
    });
}(jQuery || window.jQuery))
</script>

您只需要在<a>标记中使用类force-download,就会自动强制下载。您也可以将它添加到父div,并将拾取其中的所有链接。

例子:

<a href="/some/good/url/Post-Injection_Post-Surgery_Instructions.pdf" class="force-download" target="_blank">Download PDF</a>

这对于WordPress和任何其他系统或自定义网站来说都是很棒的。

其他回答

一个非常简单的方法来实现这一点,不使用外部下载网站或修改标题等,只是创建一个ZIP文件与PDF里面,并直接链接到ZIP文件。这总是会触发保存/打开对话框,人们仍然很容易双击PDF窗口,与.zip相关的程序被启动。

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

元标签并不是实现这一结果的可靠方法。一般来说,你甚至不应该这样做——应该由用户/用户代理来决定如何处理你提供的内容。如果用户愿意,总是可以强制浏览器下载文件。

如果您仍然希望强制浏览器下载文件,请直接修改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

请注意,这只是对HTTP协议的扩展;有些浏览器可能会忽略它。

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

以下内容可能会对你有所帮助。几年前我用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>";
    }
?>

将上述文件保存为download.php。

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

你可以这样使用它:

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

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

参考来源:这个博客

尝试将这一行添加到.htaccess文件中。

AddType application/octet-stream .pdf

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

只要把下面的代码放在你的。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

或者你也可以通过JavaScript来实现

element.setAttribute( 'download', whatever_string_you_want);