วันอังคารที่ 14 มิถุนายน พ.ศ. 2565

ํYii2 Upload File pdf แล้วให้คลิก Preview ได้เลย

 Models

public function listDownloadFiles($type){
        $docs_file = '';
        if(in_array($type, ['covenant'])){
                $data = $type==='docs'?$this->docs:$this->covenant;
                $files = Json::decode($data);
               if(is_array($files)){
                    $docs_file ='<ul>';
                    foreach ($files as $key => $value) {
                       $docs_file .= '<li>'.Html::a($value,['/pdpa65/download','id'=>$this->id,'file'=>$key,'file_name'=>$value]).'</li>';
                    }
                    $docs_file .='</ul>';
               }
        }
   
        return $docs_file;
       }

Gridview

 ['attribute'=>'covenant','value'=>function($model){return $model->listDownloadFiles('covenant');},'format'=>'html'],

ที่ Controllor

public function actionDownload($id,$file,$file_name){
        $model = $this->findModel($id);
         
        if (!empty($model->ref) && !empty($model->docs)) {
            if (substr($file_name, -3) == 'pdf') {
                return Yii::$app->response->sendFile($model->getUploadPath() . '/' . $model->ref . '/' . $file, $file_name, ['inline' => true]);
            } else {
                Yii::$app->response->sendFile($model->getUploadPath() . '/' . $model->ref . '/' . $file, $file_name);
            }
        }else{
            $this->redirect(['/pdpa65/view','id'=>$id]);
        }
    }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น