一、视图层
[
'attribute' => 'purchaseFile',
'hAlign' => 'center',
'vAlign'=>'middle',
//以下是加链接动作,及其样式(其他动作同理)
'format' => 'raw',
'value'=> function($model){
return Html::a($model->purchaseFile,
[
'download',
'id'=>$model->purchaseID
], [
'title' => '下载附件',
'data' => [
'method' => 'post',//一定要注明post方式!!
]
]);
}
]
二、控制器相应
//下载附件
public function actionDownload($id)
{
$model = $this->findModel($id);
if($model->purchaseFile){
$all = Purchase::find()
->where(['purchaseID'=>$id])
->asArray()
->one();
$fileName = $all['fileName'];
//查找路径并下载
if (file_exists('../uploads/purchaseFile/' . $fileName)) {
return Yii::$app->response->sendFile('../uploads/purchaseFile/' . $fileName)->send();
} else {
return "<h1>文件不存在</h1>";
}
}else{
Yii::$app->session->setFlash('error', '不存在采购附件');
return $this->redirect(['index', 'id' => $model->purchaseID]);
}
网友评论