如何解决使用zend contextSwitchAction 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代码。
我希望这有帮助。
亲切的问候
加里
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。