简体   繁体   English

我无法强制下载 pdf 文件

[英]I can't force download a pdf file

i've tried searching solutions to my problem for about 2 days now and i can't seem to make my code work.我已经尝试搜索我的问题的解决方案大约 2 天了,但我似乎无法使我的代码正常工作。 My goal is to download a PDF file on a button click but i want the path of the file to be hidden for the users.我的目标是通过单击按钮下载 PDF 文件,但我希望为用户隐藏文件的路径。 The project im working on is using Kohana(3.3) framework.我正在从事的项目正在使用 Kohana(3.3) 框架。 I've tried to call that function with ajax :我试图用 ajax 调用该函数:

public function action_download_pdf()
{
        $this->auto_render = false;
        if ($this->request->post()) {

            $data = $this->request->post();
            $file = 'uploads/pdfs_folder/'.$data['pdf_number'].'.pdf';

            if (file_exists($file)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.$data['pdf_number'].'.pdf');
                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;
            }
        }
    }

I'm getting Status Code: 200 OK, but i only get it to show in the "preview" tab on my developer console.我收到状态代码:200 OK,但我只让它显示在我的开发人员控制台的“预览”选项卡中。

https://i.stack.imgur.com/vyAiK.jpg https://i.stack.imgur.com/vyAiK.jpg

I can't figure out what should i do to dowload it instead of showing it that way ?我不知道我应该怎么做才能下载它而不是那样显示它?

This has worked for me perfectly.这对我来说非常有效。

if ($filename) {

    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=".basename($filename)); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$filename");

}

below code worked fine for me下面的代码对我来说很好用


<?php
    if(isset($_REQUEST['path']) && $_REQUEST['path'] != "") {

        $file_url = $_REQUEST['path'];
        $pdfname = basename ($file_url);
        header('Content-Type: application/pdf');
        header("Content-Transfer-Encoding: Binary");
        header("Content-disposition: attachment; filename=".$pdfname);
        readfile($file_url);
    }
?>

In your code i noticed在你的代码中我注意到

header('Content-Type: application/octet-stream');

The content-type reference in the PHP is important , it's the MIME type of the file you're protecting. PHP 中的内容类型引用很重要,它是您要保护的文件的 MIME 类型。 If, for example, you saved an MP3 file instead, you'd need to replace application/pdf with audio/mpeg.例如,如果您保存的是 MP3 文件,则需要将 application/pdf 替换为 audio/mpeg。

I hope this will help you.我希望这能帮到您。

Thanks谢谢

So this is how i solved my issue:所以这就是我解决我的问题的方法:

I made a table in my DB in which i generate and save one fake number and one real number.我在我的数据库中制作了一个表格,我在其中生成并保存了一个假数字和一个真实数字。 When i reach the step with the button to download the PDF i submit a form thru that button which triggers a function in my Controller.当我到达带有下载 PDF 按钮的步骤时,我通过该按钮提交了一个表单,该按钮触发了我的控制器中的一个功能。 The function is the code i initially posted.该函数是我最初发布的代码。 I check the fake number in my table and get the real one.我检查了我表中的假数字并得到了真实数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM