0

我使用以下代码下载文件

    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 

我用谷歌搜索了很多来解决它,但没有任何效果。

4

1 回答 1

2

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

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

于 2013-06-11T06:45:23.533 回答