Frage

Lots of questions here from people trying to implement gzip encoding in Zend - I need to do the opposite!

I have a controller which extends the standard Zend_Controller_Action. My downloadAction has a PDF file as it's response body. That works well, except that the downloaded file isn't correctly recognised by the client browsers.

The downloaded file is identified as a 'Zip Archive' by the browser download. When saved and double-clicked it opens correctly as a PDF. The response header shows Content-Encoding:gzip, so I figure that's likely the culprit.

The core of my action is:

$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);
    }

There is some other code before this piece, but it does nothing more exciting than populate the variables.

How would I go about disabling the Content-Encoding:gzip header for just this response, or if that's the wrong end of the stick (it would be good to use compression, but not at the expense of user experience), how do I get the client to correctly identify the downloaded file once the compression has been reversed?

War es hilfreich?

Lösung

I would recommend to use framework's Zend_Controller_Response_Http instead of header() function, usually I specify "default" headers with gzip compression etc. in my Bootstrap for all responses, and override them in actions for some special reasons:

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);

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top