美文网首页
YII2 在线展示pdf文件与下载

YII2 在线展示pdf文件与下载

作者: 阿_莫西林 | 来源:发表于2020-04-30 17:55 被阅读0次

一.控制器 编辑输出格式

public function actionSee($id)
    {
        $dd = substr(strrchr($id, "="), 1);//获取id,有直接函数可使用。
        $dd = (int)$dd;
        $model = $this->findModel($dd);
        if ($model->contractFile) {
            $fileName = $model->fileName;
            $path = '../uploads/contractFile/' . $fileName;
            $file = fopen($path, "r+"); // 打开文件
            // 输入文件标签
            header('Content-type: application/pdf');
            header('filename:'.$file);
            // 输出文件内容
            echo fread($file, filesize($path));
            fclose($file);
        }
    }

二.在视图层触发

'see' => function ($url) {
        return Html::a('<span class="">预览</span>',
            ['contract/see','id' =>$url],
            [
            'title' => '在线预览',
            'data' => [
                    'method' => 'post',
            ],
            'target' => '_black' 
        ]);
    },

相关文章

网友评论

      本文标题:YII2 在线展示pdf文件与下载

      本文链接:https://www.haomeiwen.com/subject/arstghtx.html