Yii2 SendFile()显示一些字符而不是下载

时间:2017-09-01 15:08:21

标签: php yii yii2

我有控制器代码:

 public function actionDownload($id) {

        $download = Status::findOne($id);

        $path = Yii::getAlias('@webroot').'/uploads/status/'.$download->image_web_filename;

        if (file_exists($path)) {
            return Yii::$app->response->SendFile($path,$download->image_src_filename);
           
            exit;
        } else {
            throw new NotFoundHttpException("can't find {$download->image_src_filename} file");
        }
    }

我在GridView中有视图代码:

 ['attribute'=>'Download',
       'format'=>'raw',
        'value' => function($data)
        {
        return
        Html::a('Download file', ['status/download','id'=>$data->id],['class' => 'btn btn-primary']);

        }
        ],

这是屏幕上显示的内容。只有当我刷新浏览器时才会下载pdf,但屏幕上仍然会出现问题是什么?:

%PDF-1.4 %�쏢 5 0 obj <> stream x��]K�%ECű%P4���y��.�u�@�Q �zt��P�0��_��'��gx����/3�n���zܞ���xaAR7o�s�<�;�����܈�o��w?8��]�y�/'�O�FU��@b㍤���G����͇'r����4�+B��{3���SW����'��<�������A�ǃ*x���N27r�$���V������w�_?��k�~���+�'wFr6��f! ��ȍ7�Ӡ���UG���g;=�2�j��Ǒ �oƱQƇfh�?�WҘ�0۟��/�l�E���i(�{��* ��'�W��8��T�j 2��p������\��aܵH�$�Ci��u��$j��QZ���aIk����\o���D2Y)�/FV��nġ�a����� �_ؐ��rf�� �7�*� �IN�J{�l����Cqr�D�������[�]9���E�7�����k�j���9N�\�EC��F����&ռ-����uJV����3����L!�f��x���F�����̈́��#��U����:*g��&C�ӤD��q�17�@Jg+�J����w��ߨ?M_�a���R�ݼ��zu%��\�#�7$M&���-dO"#�]�i�89�}��<��� m��04�I�bHJ �O�M��`f�Ϯ�E�{2}`���n�����.l���+cZ?�����6C��N��XZJW�ylȒ��"�D�ͧ�8s���q��o׉m��ݍx�6(��T�9Uɨ�g���x���\���'�Ͻ����BE��|��� p���9P]��R*����C��������jC�m���1��#K���׶Y���rt=�:�x����W����qr�便p4��"��B�}=�2������p��!x+�)zv���̳���\^*�R�ugȎ!J<��q�~���C��/��ˏU8�C7A0`�6x����6ۗ#�b���Ħ�(��Sk̍�"|����9�" �G�`@LD���e�B�Q���� d��[q�Z�aځ��-j�����Zk�}F4���l���y�T ��B��|���ҕ~/����a�+�7��Ai=W���1�

3 个答案:

答案 0 :(得分:0)

看起来你需要标题。

尝试使用此代码:

header("Content-Disposition:attachment;filename='filename.pdf'");

如果不起作用,请尝试添加此标题

{{1}}

答案 1 :(得分:0)

我在这个特定代码中遇到的问题是我必须删除    上面的<?php Pjax::begin(); ?><?php Pjax::end(); ?>以及'pjax'=>true以及gridview列,以便它可以工作我没有改变actionDownload($id)代码中的任何内容

编辑之前(原始错误代码) `....................................

<h3><?= Html::encode($this->title) ?></h3>

<p>
    <?= Html::a('Create Status', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>  

<?php

$gridColumns = [
['class' => 'kartik\grid\SerialColumn'],
'id',
'message',
'permissions',
'created_by',
'created_at',
// 'updated_at',
// 'image_src_filename',
//'image_web_filename',

 ['attribute'=>'Download',
   'format'=>'raw',
    'value' => function ($data) {
        return
        Html::a('Download file', ['status/download','id'=>$data->id], ['class' => 'btn btn-primary']);
    }
    ],
];
?>
<?php
$export = ExportMenu::widget([
'dataProvider' => $dataProvider,
//'columns' => $gridColumns,
'fontAwesome' => true,
'exportConfig' => [
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_TEXT => false,
ExportMenu::FORMAT_CSV => false,
ExportMenu::FORMAT_EXCEL => false,
ExportMenu::FORMAT_EXCEL_X => false,
],
]);
?>

<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'containerOptions' => ['style' => 'overflow: auto'], // only set when $responsive = false
'toolbar' => [
$export
],
'pjax' => true,
'bordered' => true,
'striped' => true,
'responsive' => true,
'floatHeader' => true,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => 'Forms Uploaded by Clients.',
],
]);
?>

<?php Pjax::end(); ?></div>`

编辑后(原始错误代码)

`

<h3><?= Html::encode($this->title) ?></h3>

<p>
    <?= Html::a('Create Status', ['create'], ['class' => 'btn btn-success']) ?>
</p>

<?php

$gridColumns = [
['class' => 'kartik\grid\SerialColumn'],
'id',
'message',
'permissions',
'created_by',
'created_at',
// 'updated_at',
// 'image_src_filename',
//'image_web_filename',

 ['attribute'=>'Download',
   'format'=>'raw',
    'value' => function ($data) {
        return
        Html::a('Download file', ['status/download','id'=>$data->id], ['class' => 'btn btn-primary']);
    }
    ],
];
?>
<?php
$export = ExportMenu::widget([
'dataProvider' => $dataProvider,
//'columns' => $gridColumns,
'fontAwesome' => true,
'exportConfig' => [
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_TEXT => false,
ExportMenu::FORMAT_CSV => false,
ExportMenu::FORMAT_EXCEL => false,
ExportMenu::FORMAT_EXCEL_X => false,
],
]);
?>

<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'containerOptions' => ['style' => 'overflow: auto'], // only set when $responsive = false
'toolbar' => [
$export
],
'bordered' => true,
'striped' => true,
'responsive' => true,
'floatHeader' => true,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => 'Forms Uploaded by Clients.',
],
]);
?>`

多数民众赞成!我发现了。

答案 2 :(得分:0)

只需添加

 ob_clean();

之前

return Yii::$app->response->SendFile($path,$download->image_src_filename);

。 ob_clean()将清除您的输出并解决问题。 它为我工作。 谢谢