在CakePHP中实现文件下载

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

猜你喜欢

转载自blog.csdn.net/hwhsong/article/details/78331512
今日推荐