我已成功上传文件(pdf),但我想知道点击链接时如何显示该文件。
以下是我的代码: 1. FileContoller.php
public function actionCreate()
{
$model=new File;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['File']))
{
$simpan=$model->nama_file=CUploadedFile::getInstance($model,'nama_file');
if(empty($simpan)){
$model->attributes=$_POST['File'];
$model->save();
}
else{
$model->attributes=$_POST['File'];
$model->nama_file = CUploadedFile::getInstance($model, 'nama_file');
if($model->save()){
$simpan->saveAs(Yii::app()->basePath .
'/../files/' . $model->nama_file.'');
$this->redirect(array('view','id'=>$model->id_file));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
在/protected/views/file/view.php
下的view.php <?php $this->widget('bootstrap.widgets.TbDetailView',array(
'data'=>$model,
'attributes'=>array(
'id_file',
'nama_file',
'deskripsi',
'id_user',
'id_kategori',
'tgl_post',
),
)); ?>
怎么做?请帮帮我,谢谢你回答我。
答案 0 :(得分:0)
您可以通过链接点击控制器/操作请求。 例如: //这只是一个示例代码。请更改它以适合您
$this->widget('zii.widgets.TbDetailView', array(
'data'=>$model,
'attributes'=>array(
'title', // title attribute (in plain text)
'owner.name', // an attribute of the related object "owner"
'description:html', // description attribute in HTML
array( // related city displayed as a link
'label'=>'City',
'type'=>'raw',
'value'=>CHtml::link(CHtml::encode($model->city->name),
array('city/view','id'=>$model->city->id)),
),
),
));
并在那里添加以下代码: //这只是一个示例代码。请更改它以适合您
$length = filesize($yourfile)
header("Content-type: application/pdf");
header("Content-Length: $length");
header("Content-Disposition: inline; filename=sth.pdf");