Thursday, February 2, 2017

CakePHP2 CakeResponse::file() does not start download in Firefox

Leave a Comment

I use CakeResponse::file() to let users download a file, as described here.

https://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file

However, I noticed it works fine in Chrome, IE, Opera, etc., except Firefox. Just nothing happens in Firefox.

$this->response->file( $filepath, [     'name' => $filename,     'download' => true, ] ); return $this->response; 

My controller's code is essentially like this. It seems file type is irrelevant. What's wrong with Firefox?

1 Answers

Answers 1

You should give a link to download file like this, it will work in all browser. TESTED

<?php echo $this->Html->link('Download',    array('controller' => 'Home',   'action'=>'download')); ?> 

Code in your Home controller

  public function download() {     $this->response->file( $filepath, [      'name' => $filename,      'download' => true,     ] );    return $this->response;   } 

If you call this download functon by ajax it will show the file in response instead of downloading.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment