Reputation: 75
I have the controller code:
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");
}
}
I have the view code in the GridView:
['attribute'=>'Download',
'format'=>'raw',
'value' => function($data)
{
return
Html::a('Download file', ['status/download','id'=>$data->id],['class' => 'btn btn-primary']);
}
],
This is what displayed on the screen.The pdf is only downloaded when the I refresh the browser but this still remains on the screen what is the problem?:
%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��om��ݍ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�
Upvotes: 1
Views: 1378
Reputation: 1
Try to add data-pjax="0"
to the <a>
tag. By adding data-pjax="0"
to the link, you're instructing PJAX to ignore this specific link, allowing the browser to handle the download as a regular HTTP request.
Upvotes: 0
Reputation: 67
Just add
ob_clean();
before
return Yii::$app->response->SendFile($path,$download->image_src_filename);
. ob_clean() will clean your output and fix the problem . It worked for me. Thanks
Upvotes: 1
Reputation: 75
The problem that I had in this specific code is that I had to remove
<?php Pjax::begin(); ?>
and <?php Pjax::end(); ?>
and also 'pjax'=>true
above and in the gridview column so that it worked I didnt alter anything in the actionDownload($id)
code
Before editing(original erroneous code) `....................................
<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>`
After editing(original erroneous code)
`
<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.',
],
]);
?>`
Thats all !! I found out.
Upvotes: 1
Reputation: 459
It is look like you need header for that.
try with this code:
public function actionDownload($id) {
header("Content-type:application/pdf");
........
if is not working try to add this header too
header("Content-Disposition:attachment;filename='filename.pdf'");
Upvotes: 0