【问题标题】:php prompt user to download pdfphp提示用户下载pdf
【发布时间】:2015-09-16 20:25:37
【问题描述】:

我从角度 $http.get 请求中接收到二进制 pdf 文件。我正在尝试打开一个对话框以提示用户使用 readfile() 下载文件,但没有显示任何对话框。另外,有没有办法只使用二进制数据,而不将其写入文件系统以与 readfile() 一起使用,以提示用户下载文件?

return $http({
            method: 'get',
            url: env.baseUri + '/1.0/releases/report/file?regions=' + regionIds
        })
        .then(
            function (response) {
                getPdfFactory.getPdf(response.data)
                .then(function(pdf){
                   // replace with php
                    //window.open("data:application/pdf;base64, " + pdf.data);
                    ...


<?php 
    if(isset($_POST)) {
        $postdata = file_get_contents("php://input");
        $binary = json_decode($postdata);
  //       $base64pdf = base64_encode($binary);  
        file_put_contents("../../../src/uploads/report.pdf", $binary);
  //           echo $base64pdf;

        $file = "../../../src/uploads/report.pdf";
        header('Content-Description: File Transfer');
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
?>

【问题讨论】:

    标签: php angularjs pdf


    【解决方案1】:

    我为发布数据创建了一个表单并将操作设置为 php 脚本,从而解决了无法将文件下载到用户计算机的问题。问题在于从 ajax 发布请求向 php 脚本提交数据,其中响应将在 php 下载过程发生之前返回给 ajax 进程。

    【讨论】:

      猜你喜欢
      • 2010-12-20
      • 2017-04-04
      • 2021-02-27
      • 1970-01-01
      • 2022-12-05
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多