【问题标题】:Cakephp 2, response body content type not workingCakephp 2,响应正文内容类型不起作用
【发布时间】:2018-04-25 20:44:09
【问题描述】:
$this->autoRender = false;
$this->response->header("HTTP/1.1 200 OK");
$this->response->header("Pragma: public");
$this->response->header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
$this->response->header("Cache-Control: private", false);

//$this->response->header("Content-type: image/png"); //tried also
$this->response->type('jpg');

$this->response->body(readfile($file));

return $this->response;

总是返回 Content-Type: text/html;字符集=UTF-8。

谢谢

【问题讨论】:

  • 内容类型不对,但是body本身呢?它是否包含您期望的图像?也许代码中还有另一个问题,而是生成了错误响应。那么是否有任何错误显示?你检查过日志文件吗?
  • 日志中没有错误。 (蛋糕的临时日志,apache 都没有日志)图像正确显示,如果它们在页面内。但是,如果我在新选项卡(使用相同的链接)中打开图像,由于内容类型错误,图像不会显示为图像,而是显示为 ascci(我相信)表示(二进制的 scren 中的经典抛出)。有任何想法吗?我错过了一些步骤吗?
  • 您在控制器中加载了哪些组件?
  • use MicrosoftAzure\Storage\Common\Internal\Resources; use MicrosoftAzure\Storage\File\FileRestProxy; use MicrosoftAzure\Storage\File\FileSharedAccessSignatureHelper; App::uses( 'AuthController', 'Controller' ); App::uses( 'Sanitize', 'Utility' ); 你是这个意思吗?谢谢
  • 您是否尝试加载RequestHandler 组件以查看是否有帮助?

标签: php cakephp http-headers httpresponse cakephp-2.8


【解决方案1】:
$this->response->body(readfile($file));

将此行改为

$this->response->body(file_get_contents($file));

并且有效。

我知道它是更高性能的 readfile,但目前应该可以工作。

readfile 可能会因结尾而中断。

【讨论】:

  • 啊,确实。我完全错过了。 readfilefile_get_contents 以非常不同的方式工作:)。只有第二个返回文件的内容,而第一个返回一个 int 值!
猜你喜欢
  • 2017-09-17
  • 2016-05-17
  • 1970-01-01
  • 2012-12-12
  • 2011-08-27
  • 2013-10-23
  • 2014-01-14
  • 2011-08-21
  • 2015-07-27
相关资源
最近更新 更多