【问题标题】:Why can't i trigger browser download dialog using zend framework action?为什么我不能使用 zend 框架操作触发浏览器下载对话框?
【发布时间】:2013-01-16 12:24:25
【问题描述】:

我构建了一个小脚本来触发下载对话框,如下所示:

<?php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.'123.txt');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');

ob_end_clean();
echo ' '; 
flush();

//do something long
//for example
sleep(5);
for($i = 0; $i<10; $i++){
echo $i . PHP_EOL;
sleep(1);             
}

如果我将它放入一个名为download.php 的文件中,它会正常运行;立即弹出下载对话框。

如果我将它放入 Zend Framework Action 并禁用布局和视图渲染,它的行为会有所不同(大约 15 秒后会弹出下载对话框)。

为什么会有这种差异?

【问题讨论】:

    标签: php zend-framework http-headers


    【解决方案1】:

    Zend Framework 处理动作的输出,并会添加额外的标头并可能覆盖标头。

     // Disable default phtml rendering
     $this->_helper->viewRenderer->setNoRender(true);
     $response->clearAllHeaders();
    

    对于 ZF1,我们使用此行来关闭视图的正常渲染以允许下载工作。

    【讨论】:

    • 我已经用过 $this->_helper->viewRenderer->setNoRender(true);我试图添加 $this->_response->clearAllHeaders();但没有任何改变。
    猜你喜欢
    • 2011-11-22
    • 2012-07-11
    • 2019-12-21
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    相关资源
    最近更新 更多