Cakephp 3.0下载文件

时间:2015-05-16 07:24:55

标签: cakephp-3.0

之前我问了一个关于文件上传的问题。我设法让它工作,我的下一步是让客户下载文件,文件可能是从视频到PDF的不同格式。

我确实读过有关媒体观点的内容,但是他们认为是cakephp 2.0。我发现2.0似乎至少对我造成了问题。

如果有帮助,我可以上传我的文件进行上传。

基于Alex发布的链接,如何在我的视图中调用此函数这是基于cakephp图书链接的函数

public function downloadFile($id)
{
   $file = $this->Attachments->getFile($id);
   // a view.
   $this->response->file(
      $file['files/content'],
      ['download' => true]
   );
   return $this->response;
}

2 个答案:

答案 0 :(得分:1)

要获取该功能的链接(使用cakephp默认路由),您可以在视图中执行此操作:

echo $this->Html->link('Download File', ['controller' => 'Attachments', 'action' => 'download_file', $id]);

答案 1 :(得分:0)

public function download(){
    $file_path = WWW_ROOT.'uploads'.DS.'file_name.doc';
    $this->response->file($file_path, array(
        'download' => true,
        'name' => 'file_name.ppt',
    ));
    return $this->response;
}