使用zend contextSwitch()Action Helper下载文件

|| 大家我和类似的东西叠在一起:我必须提供Zend Framework的下载文件功能...几个小时的谷歌搜索对我没有帮助... 所以这是我的控制器代码(注意:我是初学者):
//callback
public function sendFile()
{
    readfile(APPLICATION_PATH . \"/../public/pdf/10.pdf\");
}

public function init()
{
    $this->_helper->contextSwitch()
            ->addContext(\'file\', array(
                  \'headers\' => array(
                      \'Content-Type\'          => \'application/pdf\',
                      \'Content-disposition\'   => \'attachment; filename=\"10.pdf\"\'),
                  \'callbacks\' => array(
                      \'init\'  => \'_sendFile\'
                  )
              ))
            ->addActionContext(\'download\', \'file\')
            ->setAutoJsonSerialization(false)
            ->initContext();

}
// ...
public function downloadAction()
{
}
PS:我发现此下载文件带有zend框架,但我想使用Zend方法。 谢谢你们     
已邀请:
        你可以试试看。
public function init()
{
    $this->_helper->contextSwitch()
            ->addContext(\'file\'))
            ->addActionContext(\'download\', \'file\')
            ->setAutoJsonSerialization(false)
            ->initContext();

}
// ...
public function downloadAction()
{
    if ($this->_helper->contextSwitch()->getCurrentContext() == \'file\') {
        $this->getResponse()
                ->setHeader(\'Content-type\', \'application/pdf; charset=binary\')
                ->setHeader(\'Content-Disposition\', \'attachment; filename=\"10.pdf\"\')
                ->setHeader(\'Content-length\', filesize(APPLICATION_PATH . \"/../public/pdf/10.pdf\"))
                ->setHeader(\'Cache-control\', \'private\');
        readfile(APPLICATION_PATH . \"/../public/pdf/10.pdf\");
        $this->getResponse()->sendResponse();
    } else {
        throw new Zend_Controller_Action_Exception(\'File not found\', 404);
    }
}
例如,您还必须在调用页面中将参数格式设置为POST或GET变量形式的文件。
<a href=\"http://yourdomain.com/path/to/controller/format/file\">
如果您的文件位于公共文档文件夹中,则只需直接链接即可。
<a href=\"http://yourdomain.com/pdf/10.pdf\">
而不用理会上面的PHP / ZF代码。 我希望这有帮助。 亲切的问候 加里     

要回复问题请先登录注册