Cakephp响应对象路由



我正在使用Cake 2.3,我有一个应用程序,允许用户添加产品和上传与该产品相关的文件。我在使用响应对象时遇到了一个问题,如下所示:http://book.cakephp.org/2.0/en/controllers/request-response.html

public function download(){
  $path = WWW_ROOT.$product['Product']['filename']; //gets the path-to-file 
  $this->response->file($path, array('download' => true, 'name' => 'Filename'));
  return $this->response; //as per the docs
}

在我看来:

<?php echo $this->Html->link('Download', array('controller' => 'products', 'action' => 'download', $product['Product']['filename']));?>

所以这是我的问题:该文件位于localhost/app/webroot/invoices/example.pdf下,然而,文件的下载链接调用localhost/products/sendFile/invoices/example.pdf。

当我调试$path时,它给了我正确的文件URL,所以我得出的结论是下载函数搞砸了一些东西。

我可以通过localhost/app/webroot/invoices/example.pdf的URL直接在浏览器中访问文件。

为什么下载函数没有路由到正确的文件URL进行下载?

您正在向下载操作发送一个额外的参数,但您没有对它做任何操作。

将下载功能更改为:

public function download($filename){
  //The $path is probably wrong, or did you upload the file to the webroot and not into a directory in the webroot?
  $path = WWW_ROOT.$filename; //gets the path-to-file 
  $this->response->file($path, array('download' => true, 'name' => 'Filename'));
  return $this->response; //as per the docs
}

最新更新