問題描述
我正在嘗試將數據庫表導出為可從瀏覽器下載的 .csv.我的代碼是基于 zend 框架的,我幾乎可以通過以下操作實現:
I'm trying to export a database table as a .csv downloadable from the browser. My code is zend framework based and I'm almost there with the following action:
public function exportTableAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$fileName = $this->_getParam('fileName');
$tableName = $this->_getParam('tableName');
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileName.'"');
echo $this->getCsv($tableName, $fileName);
}
我可以下載包含有效數據的 .csv 文件.但是,即使我禁用了布局和渲染器,我仍然會在 .csv 文件的末尾獲得頁面的頁眉、側邊欄和頁腳的輸出.有沒有辦法禁用除我的 exportTableAction 中生成的輸出之外的任何 html 輸出?或者我可以用不同的方式將標題信息和 csv 字符串發送到瀏覽器嗎?
I can download my .csv file containing valid data. However, even if I disabled the layout and the renderer, I still get the output of the header, sidebar, and footer of my page at the end of my .csv file. Is there a way to disable any html output other than the one generated in my exportTableAction? Or can I send the header information and the csv string to the browser in a different way?
順便說一句:我正在使用操作堆棧插件來幫助我呈現標題和側邊欄,如下所示:
BTW: I'm using the action stack plugin to help me render the header and sidebar as follows:
...
$actionStack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');
$actionStack->pushStack($userlogAction);
$actionStack->pushStack($rightcolAction);
干杯,阿德里安
推薦答案
我們找到了問題的解決方案.我替換了以下行
We found a solution to the problem. I replaced the following line
$this->_helper->viewRenderer->setNoRender();
由
$this->_helper->viewRenderer->setNeverRender();
如果使用 setNeverRender(),則不會渲染任何視圖(也不來自插件).
If setNeverRender() is used, no views are rendered (from plugin neither).
這篇關于在 Zend 框架中導出 csv的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!