Cakephp 2.3.x发送文件并强制下载mp4文件 [英] Cakephp 2.3.x Sending Files and forcing the download of a mp4 file
问题描述
我正在使用cakephp 2.3.1
我要强制下载一个mp4档案,每 http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file
在我的'view'我有以下代码,它正确地搜索文件名,并找到文件名,并显示下载链接:
<?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'即将推出:4月16日可用;
}
?>
当用户点击链接时,我想强制下载mp4文件。在我的控制器我有以下代码不工作:
public function sendFile($ id){
$ file = $ this-> Attachment-> getFile($ id); //注意:我不明白'Attachment'和'getFile($ id)'
$ this-> response-> file($ file ['webroot / files /'],array下载'=> true,'name'=>'Dance'));
//返回reponse对象以防止控制器尝试呈现视图
return $ this-> response;
}
我不明白'Attachment'和'getFile / p>
我得到以下错误:
错误:调用成员函数getFile()在非对象
$
p>你不明白的那一行只是示例的一部分 - 假设应用程序有一个名为
Attachment
的模型,并且它有一个名为 getFile
。因为你没有一个 Attachment
模型(或至少它是不可见的控制器)你得到一个调用成员函数非对象错误。这不重要,虽然:你需要担心的是提供一个完整的系统路径 this-> response-> file()
。在你的例子中,你可能通过改变这一行到: $ this-> response-& WWW_ROOT.'files /'。$ id。'。mp4',array('download'=> true,'name'=>'Dance'));
您可以移除 $ this-> Attachment-> getFile
行,因为它与您的情况无关。
让我知道如果有帮助!
I'm using cakephp 2.3.1
I want to force download a mp4 file per http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file
In my 'view' I have the following code which is correctly searching for the filename, and finding the file name, and displaying the download link:
<?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';
}
?>
When the user clicks on the link I want to force the download of the mp4 file. In my controller I have the following code that doesn't work:
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;
}
I don't understand 'Attachment' and the 'getFile()'
I'm getting the following error: Error: Call to a member function getFile() on a non-object
What am I doing wrong and is there any other documentation I can be looking at to understand this better?
The line you don't understand is simply part of the example - it assumes the application has a model called Attachment
and that it has a method called getFile
. Since you don't have an Attachment
model (or at least it isn't visible to the controller) you get a "call to member function on a non-object" error. That's not important though: all you need to worry about is providing a full system path to this->response->file()
. In your example, you can probably get that by changing that line to:
$this->response->file(WWW_ROOT.'files/'. $id .'.mp4', array('download' => true, 'name' => 'Dance'));
You can get rid of the $this->Attachment->getFile
line as it is irrelevant in your case.
Let me know if that helped!
这篇关于Cakephp 2.3.x发送文件并强制下载mp4文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!