如何在zend框架中下载应用程序

时间:2012-05-16 06:10:07

标签: zend-framework

我正在使用zend框架作品。我想在我的应用程序中包含文件下载部分。我正在使用此代码

> header('Content-Type: application/doc'); header('Pragma: no-cache');
> header('Content-Disposition: attachment; filename="'.$resume.'"');
> readfile(RESUME_PATH_WS . $resume);

但是这段代码不起作用。它返回0byte的文件。请帮我如何在zend框架工作中下载文件

1 个答案:

答案 0 :(得分:1)

     public function downloadAction() {

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

            $filename = $this->_request->getParam('filename');

            $filePath = folder/path to file/ . $filename;

            if (file_exists($filePath)) {
                $fileName = basename($filePath);
                $fileSize = filesize($filePath);


                header("Cache-Control: private");
                header("Content-Type: application/stream");
                header("Content-Length: " . $fileSize);
                header("Content-Disposition: attachment; filename=" . $fileName);


                readfile($filePath);
                exit();
            } else {
                die('The provided file path is not valid.');
            }
        }

只需在你的HTML方面

<a href="path to your controller/downloadaction/filename/<name of file you want to download>">Download</a>