CakeResponse下载文件不工作 [英] CakeResponse Download file is not working

查看:141
本文介绍了CakeResponse下载文件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cakePHP 2.x编写一个简单的应用程序。我需要允许用户上传和下载文件。

I am writing a simple application with cakePHP 2.x. I need to allow user to uplaod and download files. The Upload works fine, but the i'm stuck with the download action.

我有一个控制器名称Documents,包含下载操作:

I have a Controller name Documents, containing the Download action:

public function download($id = null) {
    $this->Document->recursive = -1;
    $doc = $this->Document->find('first', array('conditions' => array('Document.id' => $id)));

    $filename = $doc['Document']['file_file_name'];

    $resp = new CakeResponse();
    $resp->download(Configure::read('upload_dir').'upload'.DS.$id.'_'.$filename);
    $resp->send();
}

是的,我没有检查文件是否存在,它只是为了测试。
所以下载方法中的路径是:/ home / www-app / upload / $ id_ $ filename

Yes I didn't check if the file exists, etc... but it's just for testing. So the path in the download method is something like: /home/www-app/upload/$id_$filename

当然,文件存在,两个路径是相等的。

Of course, the file exists and both paths are equal.

但我从chrome有以下错误:

But I got the following error from chrome:

Erreur 6 (net::ERR_FILE_NOT_FOUND) : File or Directory not found

resp-> file()方法,但是cakePHP似乎不知道该方法。

I tried the $resp->file() method but cakePHP seems to not know that method.

谢谢!

推荐答案

你不能使用Cake2.x和它的响应类你应该的方式新实例,已经有一个你需要使用:

dont use a new instance, there already is one you need to use:

$this->autoRender = false;
$this->response->send();

等。

false

修正2013-01-10: $ b $你甚至不需要send()。 autoRender部分本身就足够了。
然后响应类将在调度过程结束时自动调用send():

Correction 2013-01-10: You dont even need the send(). the autoRender part itself should suffice. The response class will then automatically invoke send() at the end of the dispatching process:

$this->autoRender = false;
$this->response->file(Configure::read('upload_dir').'upload'.DS.$id.'_'.$filename);

// optionally force download as $name
$this->response->download($name);

这篇关于CakeResponse下载文件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆