如何在Joomla 3中强制下载文件?

时间:2018-04-28 04:35:14

标签: download joomla3.0 joomla-module

为Joomla写了一个自定义模块。用户在文本字段中输入文件名,并且应下载带有输入值作为名称的pdf文件。但是当点击该页面时,页面将被重定向到主页。这是代码

$ file = $ _POST ['posttext']。 '' 。 'PDF';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; 
    filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}

尝试添加ob_flush()作为提问者建议但没有好处。该代码完美地作为一个单独的PHP文件。

1 个答案:

答案 0 :(得分:0)

尝试以下

    $app = JFactory::getApplication();
    $file = $app->input->get('posttext') . '.pdf';
    if (JFile::exists($file)) {
            // File headers
            header("Content-type: application/pdf");
            header("Content-Disposition: attachment");

            // File contents.
            readfile($file);
    }

如果您仍然遇到问题,可以检查Joomla中是否存在该文件,并使用window.open()

在新标签/窗口中下载该文件