我想在TbGridView中添加链接。点击链接显示下载选项时。
这是我的代码,
在视野中,
$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'project-documents-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'class'=>'CLinkColumn',
'label'=>'id_post_author',
'urlExpression'=>'"/projects/documents/Download/$data->files"',
'header'=>'Author'
),
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{delete}'
),
),
));
$ data->文件返回" uploads / projects / filename1.png";
控制器路径中的功能是项目/文档/下载
在我的控制器中
public function actionDownload($name){
$filecontent=file_get_contents('uploads/projects'.$name);
header("Content-Type: text/plain");
header("Content-disposition: attachment; filename=$name");
header("Pragma: no-cache");
echo $filecontent;
exit;
}
但是,我有一个错误。从TbGridView下载文件的任何其他方式。
项目是一个模块,文档是控制器,下载是一个功能
答案 0 :(得分:0)
您的操作希望您发送参数name
尝试以get格式指定名称
'urlExpression'=>'"/projects/documents/Download/?name=$data->files"',
或以yii(GET)需要的方式格式化url和参数
答案 1 :(得分:0)
// Controller Code
public function actionDownload($name = "")
{
header('Content-Description: File Transfer');
header("Content-Type: image/png");
header('Content-Disposition: attachment; filename=' . $name);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile('uploads/projects' . $name)
}
答案 2 :(得分:0)
试试这个,
<?php
public function Download($fullpath){
$filename=$fullpath;
//$fullpath like /uploads/sample.txt
$path = Yii::getPathOfAlias('webroot').$filename;
if(!empty($path)){
header("Content-type:*/*"); //for all file
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header('Content-Length: ' . filesize($path));
readfile($path);
Yii::app()->end();
}
}
?>