当响应较大(>;4kB)时,在CakePHP中设置内容类型

当响应较大(>;4kB)时,在CakePHP中设置内容类型,cakephp,cakephp-2.1,content-encoding,Cakephp,Cakephp 2.1,Content Encoding,很简单,我试图从CakePHP控制器生成并下载一个CSV文件。生成CSV没有问题,在响应>=4096字节之前一切都正常 以下控制器说明了该问题: class TestTypeController extends Controller { public function csv($size = 100) { # set the content type Configure::write('debug', 0); $this->autoRender = false;

很简单,我试图从CakePHP控制器生成并下载一个CSV文件。生成CSV没有问题,在响应>=4096字节之前一切都正常

以下控制器说明了该问题:

class TestTypeController extends Controller {
  public function csv($size = 100) {
    # set the content type
    Configure::write('debug', 0);
    $this->autoRender = false;
    $this->response->type('csv');
    # send the response
    for ($i = 0; $i < $size; $i++)
      echo 'x';
  }
}
当我呼叫
http://baseurl/test_type/csv/4096
,文件将打印到屏幕上,响应标题为:

HTTP/1.1 200 OK
Date: Tue, 05 Jun 2012 14:28:56 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.1
Content-Length: 4095
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: text/csv; charset=UTF-8
HTTP/1.1 200 OK
Date: Tue, 05 Jun 2012 14:28:53 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.1
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 38
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
显然,4kB是内容编码开始压缩响应的极限。我不熟悉内容类型的反应方式,但我显然更希望它保持text/csv

使用
RequestHandlerComponent
管理响应类型时也会出现同样的问题


我使用的是CakePHP2.2.0-RC1,但我已经验证了stable 2.1.3中存在的问题。有什么想法吗?指向正确方向的指针?

答案很简单——控制器应该返回CSV数据,而不是回显它

我翻了翻,没有发现Cake会根据内容长度更改标题。我觉得这可能是apache配置的问题。查看此链接,查看您的配置是否正在添加/更改我在尝试回显控制器中的css文件时遇到的同一问题的类型。一旦css超过4095个字符,内容类型就会从text/css变为text/html。你有没有想出一个解释?正如您所提到的,解决方案是在视图中而不是在控制器中回显css。