简体   繁体   中英

Cakephp 2, response body content type not working

$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;

Always returns Content-Type: text/html; charset=UTF-8.

Thanks

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

Change this line to

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

And works.

I know that it is more performant readfile, but at the moment, should work.

Probable something that readfile breaks with the enconding.

You can download a file more easily with:

$this->autoRender = false;
$this->response->file($path, array('download' => true, 'name' => $name));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM