【发布时间】:2014-03-26 20:49:41
【问题描述】:
我在返回 Web 服务时遇到问题, 标题看起来像这样:
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$this->filename.".csv".'"');
我收集数据然后就这样返回:
foreach($this->output_data as $each_table) {
foreach($each_table as $each_row) {
foreach($each_row as $each_key => $each_value){
$string .= Sanitize::escape($each_value).$this->delimiter;
}
$string .= "\n";
}
}
print($string);
我希望 csv 文件作为流返回。但返回的是“Firefox HTML 文档”。 我不明白。
我已经关闭了控制器中的视图渲染。 我能做什么?
【问题讨论】:
-
cakephp 是什么版本?在 2.x 中,您不直接使用 header(),而是使用响应类。
-
另外,一定要禁用'autoRender' ($this->autoRender = false);如果您不使用视图文件来输出结果。如果您使用的是 CakePHP 2.x;可以在此处找到有关响应类的更多信息(如@mark 所建议); Dealing with content types
-
它的 2.4。你给了我正确的提示。我已经重构了代码
-
$this->response->header(array('Content-Disposition: attachment; filename="'.Configure::read('reporting_config')['file_name'].'"' ) ); $this->response->type('application/text-csv'); print($this->send_method->get_output_data());
-
也许 cakePHP 会将您重定向到错误页面(在视图/错误中:error500.ctp 或 error400.ctp)。您是否尝试过在网络浏览器中访问该页面并验证输出是否正确显示?
标签: php web-services cakephp csv