简体   繁体   中英

Yii Download File from CGridView

I have a form on Yii that allows people to upload a variety of different file formats. When the file is uploaded, I have a Db that adds an entry for that file:

+--------------+---------------+-------------+---------------+
|   id         |     name      |    acc_id   | date_uploaded |
+--------------+---------------+-------------+---------------+
|   1          |     test.png  |  100         | 2014-05-20   |
+--------------+---------------+-------------+---------------+

The files are held on the root folder /files/uploaded and are saved using the id ie 1-test.png.

I can then on an account show the uploaded files, by displaying those that match the account id in a cgridview.

The issue I am having is that I need a download button. I have tried a few things and do not seem to be able to get this to work. The closest I have got is using the following code

array(
        'class'=>'CLinkColumn',
        'header'=>'Download',
        'label'=>'Download',
        //'url' => '/files/uploaded/$data->id',
        'htmlOptions'=>array('width'=>'18'),
    ),

However the $data variable does not seem to be working here, I believe this is to do with the CLinkColumn. If I try UrlExpression for example, it always add index.php and my file is never resolved. Can someone point me in the right direction of how to solve this and also the best method to store and call these files.

Many Thanks

You can try this:

<?php $this->widget('ext.CGridView', array(
    'id'=>'grid-id',
    'dataProvider'=>$dataProvider,
    'filter'=>$model,
    'columns'=>array(
            array(
            'name'=>'Download',
            //'header'=>'Download',
            'type'=>'raw',
            'value'=>'CHtml::link($data->FileNameFromModel, array("files/uploaded","id"=>$data->id))',
            //'htmlOptions'=>array('width'=>'10%'),
        ),
    .....
    )
    ));

You can add a custom column for it:

'name' => 'name',
'header' => 'Download File',
'type' => 'raw',
'value' => 'CHtml::link("Click!", Yii::app()->createAbsoluteUrl("CONTROLLER/ACTION",array("id"=>$data->id)))'

By above solution, your CGridView will hold a column with Download File header, and for each row it shows Download File with releated link.

Note that, I used CreateAbsoluteUrl to create a URL. You can use your own if you have no controller/action for downloading a file.

It might also be noted that, You must put the code in columns array:

'columns' =>array(
    array(...),
)

the same problem had. it didn't help me. I had to download csv-file from protected/csv directory. I made it in another way.

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-export-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(

     /////////////////////

   array(
            'type'=>'raw',
            'name'=>'link',
            'value'=>'CHtml::link("$data->link", "http://" . $_SERVER["SERVER_NAME"] . Yii::app()->request->baseUrl . "/" . $data->link)',
        ),

    )));

This should allow to download file from Gridview:

array('name'=>'file_name', 
      'type'=>'raw',
      'value'=>'CHtml::link($data->file_name, "uploads/materials/{$data->file_name}")',
      'filterHtmlOptions'=>array('style'=>'width: 40%;')),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM