在CakePHP中实现文件下载

来源:互联网 发布:java 多线程 回调函数 编辑:程序博客网 时间:2024/06/07 10:13

在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 viewreturn $resp;