Yii 框架 - 多重下载

Yii framework - multiple download

我正在使用 Yii 框架开发数据库接口。我有一个名为 take 的 table,用于存储音频文件。我在索引页中添加了高级搜索和一个 link 来下载搜索结果的所有文件,如下所示:

<?php
/* @var $this takeController */
/* @var $dataProvider CActiveDataProvider */

$this->breadcrumbs=array(
    'Takes',
);

$this->menu=array(
    array('label'=>'Create take', 'url'=>array('create')),
    array('label'=>'Create multiple take', 'url'=>array('create_multiple')),
    array('label'=>'Manage take', 'url'=>array('admin')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').toggle();
    return false;
});
$('.search-form form').submit(function(){

    $.fn.yiiListView.update('takelistview', { 

        data: $(this).serialize()
    });

    return false;
});
");
?>

<h1>Takes</h1>

<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php  $this->renderPartial('_search',array(
    'model'=>$model,
)); ?>
</div>

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'id'=>'takelistview',
    'sortableAttributes'=>array('id', 'data', 'take_mono_nf_id', 'take_mono_ff_id')
)); ?>

<?php echo CHtml::link('Download all','#',array('class'=>'download-button')); ?>

<form style="display: hidden" action="index.php?r=take/download_several" method="POST" id="form">

</form>
<script>
    $('.download-button').click(function(){
        var i = 0;
        var indexes = [];   
        while($.fn.yiiListView.getKey('takelistview', i)){
            indexes[i] = $.fn.yiiListView.getKey('takelistview', i);

            var newdiv = document.createElement('div');
            newdiv.innerHTML = '<input type="hidden" id="indexes" name="indexes[]" value="'+indexes[i]+'"/>';
            document.getElementById('form').appendChild(newdiv);            
            i++;
        }
        newdiv = document.createElement('div');
        //newdiv.innerHTML = '<input type="hidden" id="folder" name="folder" value="'+folder+'"/>';
        document.getElementById('form').appendChild(newdiv);    
        //$("#indexes").val(indexes);
        $("#form").submit();

    });

</script>

在控制器文件中,操作 download_several 创建一个 zip 文件并下载它:

public function actionDownload_several(){
        $indexes = $_POST['indexes'];       
        $zip = new ZipArchive();
        $zip_name = "files.zip"; // Zip name
        $zip->open($zip_name,  ZipArchive::CREATE);     

        foreach($indexes as $i){
            /* query to retrieve the file name starting from the index */
            $file = Yii::app()->basePath.'/../data/'.$name.'/takes/'.$filename;
            $zip->addFromString(basename($file),  file_get_contents($file)); 
            //file_put_contents($folder.$file, file_get_contents($myfile));     //provare ftp_nb_get
        }
        $zip->close();
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename='.$zip_name);
        header('Content-Length: ' . filesize($zip_name));
        readfile($zip_name);
        unlink($zip_name);
    }

问题如下。在视图页面中,高级搜索的结果被分成页面。当我按下 "Download all" link 时,只会下载当前页面的文件。 我要下载所有结果。我很确定问题出在 $.fn.yiiListView 但我不知道要更改什么。

所以函数$.fn.yiiListView.getKey()只有returns来自当前html的索引:

/**
 * Returns the key value for the specified row
 * @param id string the ID of the list view container
 * @param index integer the zero-based index of the data item
 * @return string the key value
 */
$.fn.yiiListView.getKey = function(id, index) {
        return $('#'+id+' > div.keys > span:eq('+index+')').text();
};

分页阻止了获取所有键的功能,因为它没有加载到页面上。

要获取所有数据,您需要在视图中使用 html 发送所有索引。例如在这样的 js array 中:

<script>    
var allIndexes = [<?php echo '"'.implode('","', $allIndexes).'"' ?>]; //creates js array from php array
</script> 

上面的脚本将 PHP 数组 $allIndexes 中的值回显到 js var allIndexes

现在您需要获取 $allIndexes PHP 数组中的索引。 您可以在 Controller.

中执行此操作

例如:

$criteria = ... // create your criteria to only get the id's consistent with your ListView
$allIndexes = Take::model()->findAll($criteria); //if Take is your model name.

现在只有 post 您创建的 downloadAll onclick() 函数中 allIndexes js array 的所有索引,它应该会下载所有内容。

祝你好运!

-- 编辑--

每次单击 "next page" 时,takes 都会更新到数据库的当前状态。

我建议您在此活动中更新 js array,这样一切都始终是最新的和一致的。