0

我正在使用 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 进行下载?任何帮助将不胜感激。

4

1 回答 1

0

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

将下载功能更改为:

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
}
于 2013-05-22T09:09:37.980 回答