小编use*_*231的帖子

Cakephp 2.3.x发送文件并强制下载mp4文件

我正在使用cakephp 2.3.1

我想强制下载一个mp4文件,每个http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file

在我的"视图"中,我有以下代码正确搜索文件名,找到文件名,并显示下载链接:

<?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';
    }
?>
Run Code Online (Sandbox Code Playgroud)

当用户点击链接时,我想强制下载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;
}   
Run Code Online (Sandbox Code Playgroud)

我不明白'附件'和'getFile()'

我收到以下错误:错误:在非对象上调用成员函数getFile() …

cakephp file download

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

cakephp ×1

download ×1

file ×1