【问题标题】:Remove gzip encoding in Zend在 Zend 中删除 gzip 编码
【发布时间】:2012-04-28 04:50:04
【问题描述】:

这里有很多人试图在 Zend 中实现 gzip 编码的问题 - 我需要做相反的事情!

我有一个扩展标准 Zend_Controller_Action 的控制器。我的 downloadAction 有一个 PDF 文件作为它的响应正文。这很好用,只是客户端浏览器无法正确识别下载的文件。

下载的文件被浏览器下载标识为“Zip 存档”。保存并双击后,它会以 PDF 格式正确打开。响应头显示 Content-Encoding:gzip,所以我认为这可能是罪魁祸首。

我行动的核心是:

$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

if ($fd = fopen($pdfpath.$pdf->Filename,'r')) 
    {
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="summary.PDF"');
        while(!feof($fd)) 
        {
           $buffer = fread($fd, 2048);
           echo $buffer;
        }
        fclose($fd);
    }

在这篇文章之前还有一些其他代码,但它没有比填充变量更令人兴奋的了。

我将如何为这个响应禁用 Content-Encoding:gzip 标头,或者如果这是错误的一端(使用压缩会很好,但不会以牺牲用户体验为代价),如何反向压缩后,我如何让客户端正确识别下载的文件?

【问题讨论】:

  • 首先如何压缩内容?你有像 mod_deflate 这样的服务器端压缩你所有页面的东西吗?
  • 我认为 gzip 不是问题,但内容类型应该是 application/pdf,就像下面 b.b3rn4rd 的帖子一样。如果您将它作为八位字节流提供,它不知道它是什么类型的文件。如果 gzip 是由 Tim 所说的服务器模块完成的,在某些情况下,您将不得不调整服务器配置以禁用它,这超出了 php 的范围。
  • 我不确定 gzip 压缩的确切位置 - 在服务器级别没有指定任何内容,如果我直接从服务器下载(PHP/Zend 之外)它工作正常。它在 Zend 处理响应的某个地方。使用 gzip 编码,Firefox 和 Chrome 都无法下载。歌剧虽然有效。 b.b3rn4rd 的回答为我解决了这个问题。是 gzip 和 content-type 都给我带来了问题。

标签: zend-framework http-headers gzip content-encoding


【解决方案1】:

我建议使用框架的Zend_Controller_Response_Http 而不是 header() 函数,通常我在我的引导程序中为所有响应指定具有 gzip 压缩等的“默认”标头,并出于某些特殊原因在操作中覆盖它们:

public function indexAction()
{
      $frontContoller = $this->getFrontController(); 
      $this->_helper->layout()->disableLayout();
      $this->_helper->viewRenderer->setNoRender(true);
      $response = new Zend_Controller_Response_Http();
      $response
        ->setHeader('Content-Type', 'application/pdf')
        ->setHeader('Content-Disposition', 'attachment; filename=summary.pdf')
        ->setHeader('Expires', ''.gmdate('D, d M Y H:i:s', strtotime('31.08.1986')) . ' GMT', true)
        ->setHeader('Cache-Control', 'no-cache')
        ->setHeader('Pragma', 'no-cache', true);

      $response->setBody(file_get_contents('/full/path/to/summary.pdf'));
      $frontContoller->setResponse($response);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2011-02-11
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多