代码之家  ›  专栏  ›  技术社区  ›  Moshe

如何创建不需要右键单击的PDF下载链接?

  •  3
  • Moshe  · 技术社区  · 15 年前

    我想知道如何使它,使访问者可以简单地点击链接,而不必

    right click > Save (target) As...
    

    我对PHP和/或Javascript解决方案持开放态度。谢谢

    编辑:我可以使用javascript调用PHP并通过AJAX保存文件吗?

    6 回复  |  直到 15 年前
        1
  •  13
  •   BalusC    15 年前

    基本上你需要做的就是设置 Content-Disposition 标题至 attachment 获取“另存为”对话。下面是一个启动PHP的示例:

    <?php
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment;filename="foo.pdf"');
        readfile('/path/to/foo.pdf');
    ?>
    

    重要提示:由于功能差,在MSIE中,“另存为”对话框中的默认文件名不会从 content-disposition 标头,它将改为请求URL中pathinfo的最后一部分。要解决此问题,请将PDF文件名附加到链接,例如。 http://example.com/pdf/foo.pdf pdf.php :

    <?php
        $file_name = $_SERVER['PATH_INFO'];
        $file = '/path/to/pdf/files' . $file_name;
        if (file_exists($file)) {
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment;filename="' . basename($file_name) . '"');
            header('Content-Length: ' . filesize($file));
            readfile($file);
        } else {
            header('HTTP/1.1 404 Not Found');
        }
    ?>
    

    然而,这假设您已经 MultiViews 如此 /pdf/ RewriteRule 从…起 /pdf/ /pdf.php/ .

    <?php
        $file_name = $_SERVER['PATH_INFO'];
        $file = '/path/to/all/files' . $file_name;
        if (file_exists($file)) {
            header('Content-Type: ' . mime_content_type($file_name));
            header('Content-Disposition: attachment;filename="' . basename($file_name) . '"');
            header('Content-Length: ' . filesize($file));
            readfile($file);
        } else {
            header('HTTP/1.1 404 Not Found');
        }
    ?>
    

    说出它的名字 files.php http://example.com/files/foo.pdf http://example.com/files/bar.zip ,等等。

        2
  •  3
  •   rjmunro    15 年前

    如果您使用的是Apache,则无需编写PHP包装器脚本,您可以使用包含PDF的文件夹中的.htaccess文件来完成此操作:

    <Files *.pdf>
      Header set Content-Disposition attachment
    </Files>
    

    Content-Disposition ForceType application/octet-stream

    <Files *.pdf>
      ForceType application/octet-stream
      Header set Content-Disposition attachment
    </Files>
    
        3
  •  2
  •   ksg91    10 年前

    如果您不想弄乱服务器端代码,并且这对您来说是一件低优先级的事情,那么您可以使用HTML5 download 属性

    例子:

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

    w3schools ,支持以下浏览器:

    Chrome(14+)、Firefox(20+)、Opera(15+)。

    您还可以通过为下载属性指定值来指定文件名:

    <a href="myfile.pdf" download="NewName.pdf">Download</a>
    
        4
  •  1
  •   Amber    15 年前

    Content-type: application/octet-stream .

        5
  •  1
  •   Jeffrey Aylesworth    15 年前

    您可以添加一个HTTP头来实现这一点,在 PHP Docs

        6
  •  1
  •   Nirmal    15 年前

    以下代码可能会帮助您:

    <?php
    if(isset($_GET['docid'])){
        switch($_GET['docid']){
            case '1':
                $file = 'complete/path/to/pdf/file1';
                break;
            case '2':
                $file = 'complete/path/to/pdf/file2';
                break;
            case '3':
                $file = 'complete/path/to/pdf/file3';
                break;
            default:
                exit;
        }
    
        if(file_exists($file)){
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
    }
    

    要调用第一个pdf,请href To'/path/To/download.php?docid=1'

    要调用第三个pdf,请href To'/path/To/download.php?docid=3'

    因此,您不需要AJAX来完成这项工作。