我遇到了一个问题,我现在试图解决两天以上:我使用cakephp构建了一个网站,一切正常,但是当我试图实现下载链接到存储的文件时,我遇到了问题APP_DIR/someFolder/someFile.zip.
如何设置内部文件的下载链接someFolder?我经常偶然发现"媒体观点",我试图实施它们,但到目前为止我还没有成功.
除此之外,没有更简单的方法可以下载文件吗?
Jan*_*Jan 16
从版本2.3开始,不推荐使用媒体视图.您应该使用发送文件.
查看控制器中的这个最小示例:
public function download($id) {
$path = $this->YourModel->aMagicFunctionThatReturnsThePathToYourFile($id);
$this->response->file($path, array(
'download' => true,
'name' => 'the name of the file as it should appear on the client\'s computer',
));
return $this->response;
}
Run Code Online (Sandbox Code Playgroud)
第一个参数$this->response->file是相对于您的APP目录.所以调用$this->response->file('someFolder' . DS . 'someFile.zip')将下载该文件APP/someFolder/someFile.zip.
"发送文件"至少需要CakePHP 2.0版.另请考虑查看上面的Cookbook链接.
如果您运行的是旧版本的CakePHP,则应使用您在问题中提到的媒体视图.使用此代码并参考媒体视图(Cookbook).
以下是旧版本的相同方法:
public function download($id) {
$this->viewClass = 'Media';
$path = $this->YourModel->aMagicFunctionThatReturnsThePathToYourFile($id);
// in this example $path should hold the filename but a trailing slash
$params = array(
'id' => 'someFile.zip',
'name' => 'the name of the file as it should appear on the client\'s computer',
'download' => true,
'extension' => 'zip',
'path' => $path
);
$this->set($params);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15446 次 |
| 最近记录: |