Cakephp 3.0 下载文件

Cakephp 3.0 download file

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

我确实读过有关媒体的观点,但他们在哪里使用 cakephp 2.0。我发现 2.0 似乎至少给我带来了问题。

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

Nased 在 link Alex 发布的文章中,在我看来我将如何调用此函数这是基于 cakephp 书的函数 link

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

要获得该函数的 link(使用 cakephp 默认路由),您可以在您的视图中执行此操作:

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

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;
}