【问题标题】:Downloading PDF file with JQuery AJAX and PHP使用 JQuery AJAX 和 PHP 下载 PDF 文件
【发布时间】:2015-12-16 03:14:43
【问题描述】:

我已经在 SO 中找到了关于主题的 many questions here,但其中任何一个都可以帮助我解决特定问题。

我需要向 PHP 发送一个 ajax 请求,然后下载一个 PDF 文件。

这是我的 Javascript

$('body').on("click",'.download-file', function(e) {
        e.stopPropagation();
        var id = $(this).attr('data-id');
            $.ajax({
                url: 'app/myPage/download',// The framework will redirect it to index.php and then to myPage.class and method dowload
                data: id,
                type: 'POST',
                success: function (return) {
                   console.log(return);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                   console.log("Error... " + textStatus + "        " + errorThrown);
                }
            });
    });

这是我的 PHP 文件:

class myPage{
    public function download()
       {
            $id = $_POST['id'];
            $filePath = $this->methodToGetFile($id);
            $name = end(explode('/',$filePath));
            $fp = fopen($filePath, 'rb');
            header("Cache-Control: ");
            header("Pragma: ");
            header("Content-Type: application/octet-stream");
            header("Content-Length: " . filesize($filePath));
            header("Content-Disposition: attachment; filename='".$name."';");
            header("Content-Transfer-Encoding: binary\n");
            fpassthru($fp);
            exit;
    }
}

通过这种方法,我在控制台中“打印”了 PDF,例如

%PDF-1.5%����1 0 obj<</Type/Catalog/Pages 2 0 R/Lang(pt-BR) /StructTreeRoot 8 0 R/MarkInfo<</Marked true>>>> ....

所以,我想知道我应该怎么做才能打开这个文件并下载它。

在链接的问题中,我看到了一些关于 window.location 的内容,但我不知道如何使用它。

我已经尝试更改 PHP 标头以尝试强制下载,但没有成功。在这些情况下,我只是在 javascript 中收到 null ,没有任何反应。以下是修改后的标题:

    header('Content-Type: application/pdf');//tried with this option
    //header('Content-Type: application/octet-stream');// also tried with this other option
    header('Content-Disposition: attachment; filename=' . $name);
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filePath));

有什么好的方法可以做到这一点吗?

【问题讨论】:

  • header('Content-Type: application/pdf');而不是 header('Content-Type: application/octet-stream');
  • header('Content-type: application/pdf');你有一个小写字母 t?
  • 我认为这种下载 pdf 的方法行不通。 Javascript 正在发送请求,而 php 正在发送响应。您希望响应直接发送到浏览器,而不是 javascript。为什么不直接将下载链接直接转到下载位置,一起通过ajax?
  • 我的意思是...... onclick = window.href='app/myPage/download?'+id
  • 那么隐藏表单帖子怎么样?

标签: php jquery ajax pdf download


【解决方案1】:

我认为这种下载 pdf 的方法行不通。 Javascript 正在发送请求,而 php 正在发送响应。您希望响应直接发送到浏览器,而不是 JavaScript。您应该更改下载链接以直接转到下载位置。不需要 ajax / javascript。像这样:

<a href="javascript:void(0)" onclick = "window.href='app/myPage/download?'+id">download</a>

【讨论】:

    猜你喜欢
    • 2014-10-26
    • 2016-04-07
    • 2011-04-05
    • 2011-11-26
    • 2014-12-30
    • 1970-01-01
    • 2022-10-12
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多