CakePHP - 下载文件

时间:2013-06-08 13:25:00

标签: php cakephp download cakephp-2.0 cakephp-2.1

我使用以下代码下载文件

    public function sendFile($filePath,$filename) {             
    $this->response->file($filePath, array('download' => true, 'name' => $filename));
    return $this->response;
    }

function download($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid id for upload', true));
        $this->redirect(array('action' => 'index'));
    }
    $upload = $this->Upload->findById($id);
    if (!$upload) {
        $this->Session->setFlash(__('Invalid id for upload', true));
        $this->redirect(array('action' => 'index'));
    }
    $filename = $upload['Upload']['filename'];
    $filePath = APP.'uploads'.DS.$id.DS;
    $this->sendFile($filePath,$filename);
}

但是我收到了以下通知和错误

Notice (8): Undefined property: UploadsController::$response
Fatal error: Call to a member function file() on a non-object 

我已经搜索了很多东西来解决它,但没有任何效果。

1 个答案:

答案 0 :(得分:2)

您可以在控制器中使用下载方法

请参阅链接http://book.cakephp.org/2.0/en/views/media-view.html