Cakephp CakerResponse下载文件不工作

Cakephp CakerResponse下载文件不工作,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我正在用cakePHP 2.x编写一个简单的应用程序。我需要允许用户上传和下载文件。上传效果很好,但我一直在做下载动作 我有一个控制器名称文档,包含下载操作: public function download($id = null) { $this->Document->recursive = -1; $doc = $this->Document->find('first', array('conditions' => array('Document

我正在用cakePHP 2.x编写一个简单的应用程序。我需要允许用户上传和下载文件。上传效果很好,但我一直在做下载动作

我有一个控制器名称文档,包含下载操作:

public function download($id = null) {
    $this->Document->recursive = -1;
    $doc = $this->Document->find('first', array('conditions' => array('Document.id' => $id)));

    $filename = $doc['Document']['file_file_name'];

    $resp = new CakeResponse();
    $resp->download(Configure::read('upload_dir').'upload'.DS.$id.'_'.$filename);
    $resp->send();
}
是的,我没有检查文件是否存在,等等。。。但这只是为了测试。 因此,下载方法中的路径类似于:/home/www-app/upload/$id\u$filename

当然,文件存在并且两个路径相等

但我从chrome上得到了以下错误:

Erreur 6 (net::ERR_FILE_NOT_FOUND) : File or Directory not found
我尝试了$resp->file()方法,但cakePHP似乎不知道该方法


谢谢

您没有以您应该的方式使用Cake2.x及其响应类(以及它是如何记录的!)

不要使用新实例,您已经有一个需要使用的实例:

$this->autoRender = false;
$this->response->send();
等等

另外,如果使用autoRender false,则不需要视图(如果直接发送文件,又有什么用?)

更正2013-01-10: 您甚至不需要send()。自动转轮部分本身就足够了。 然后,响应类将在调度过程结束时自动调用send():

$this->autoRender = false;
$this->response->file(Configure::read('upload_dir').'upload'.DS.$id.'_'.$filename);

// optionally force download as $name
$this->response->download($name);