dst2007 2012-06-25 08:56
浏览 40
已采纳

PHP - Zend Framework发送文件

I am trying to send a file to the browser in a Zend Framework PHP application. I found an action helper here - https://github.com/noginn/noginn/blob/master/Noginn/Controller/Action/Helper/SendFile.php

In my controller I have an action like this

public function downloadfileAction()
{
    //get the file name and strip out all white spaces
    $title = preg_replace('/\s+/', '', $this->getRequest()->getParam('title')); 

    //create the file path          
    $path = 'downloads/'.$title.'.pdf';

    //call the action helper to send the file to the browser
    $this->_helper->SendFile->sendFile($path, 'application/x-pdf');
}

my file is located in my public folder ie MyApp/public/downloads/myFile.pdf

When I run the action, I get a view file not found error and the download is not started.

exception 'Zend_View_Exception' with message 'script 'myController/downloadfile.phtml' not found ...

Can anyone suggest what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • drju37335 2012-06-25 09:01
    关注

    It's an error about the script-(view)-file what can't be found. Cause you don't need one when sending output.

    Try disable it by this this:

    public function downloadfileAction()
    {
        //get the file name and strip out all white spaces
        $title = preg_replace('/\s+/', '', $this->getRequest()->getParam('title')); 
    
        //create the file path          
        $path = 'downloads/'.$title.'.pdf';
    
        //call the action helper to send the file to the browser
        $this->_helper->SendFile->sendFile($path, 'application/x-pdf');
        $this->_helper->viewRenderer->setNoRender();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?