在View使用超链接直接指向文件
//文件路径:webroot/files/abc.docx
$this->Html->link('下载文件', '/files/abc.docx');
在Controller设置Response类型
Cake\Http\Response::withFile( path, options = [])
//Send files as responses for your requests
$resp = $this->response->withFile('files/abc.jpg');
//Force a file to be downloaded instead of displayed in the browser
$resp = $this->response->withFile('files/abc.docx', [
'download' => true,
'name' => 'xyz.docx' //rename the file
]);
//Return the response to prevent controller from trying to render a view
return $resp;