【发布时间】:2026-01-24 04:05:01
【问题描述】:
我使用的是 cakephp 2.3.1
我想通过http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file强制下载一个mp4文件
在我的“视图”中,我有以下代码可以正确搜索文件名、找到文件名并显示下载链接:
<?php $filename = APP . 'webroot/files/' . $dance['Dance']['id'] . '.mp4';
if (file_exists($filename)) {
echo $this->Html->link('DOWNLOAD', array('controller' => 'dances', 'action' => 'sendFile', $dance['Dance']['id']));
} else {
echo 'Coming soon: available April 16th';
}
?>
当用户点击链接时,我想强制下载 mp4 文件。在我的控制器中,我有以下代码不起作用:
public function sendFile($id) {
$file = $this->Attachment->getFile($id); //Note: I do not understand the 'Attachment' and the 'getFile($id)'
$this->response->file($file['webroot/files/'], array('download' => true, 'name' => 'Dance'));
//Return reponse object to prevent controller from trying to render a view
return $this->response;
}
我不明白 'Attachment' 和 'getFile()'
我收到以下错误: 错误:在非对象上调用成员函数 getFile()
我做错了什么,是否有任何其他文档可以让我更好地理解这一点?
【问题讨论】: