php提示用户下载pdf

时间:2015-09-16 20:25:37

标签: php angularjs pdf

我从角度$ 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;
    }
?>

1 个答案:

答案 0 :(得分:0)

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