前端展示后台处理进度

来源:互联网 发布:阿里云ecs教程 编辑:程序博客网 时间:2024/06/05 10:51
将后台处理逻辑进度在用户前以进度条的方式表现出

test.php
<?php

$step = $_REQUEST['step'];
if($step == 1) {
    $re_data['step'] = array(
        array('text'=>'解压文件','url'=>'/upload/test.php?step=2','param'=>array('k'=>'v','k1'=>'v1')),
        array('text'=>'Copy文件','url'=>'/upload/test.php?step=3','param'=>array('k'=>'v','k1'=>'v1')),
        array('text'=>'Install','url'=>'/upload/test.php?step=4','param'=>array('k'=>'v','k1'=>'v1')),
    );
    $re_data['overwrite'] = array(
        'test1 file','test2 file','test3 file'
    );
    echo json_encode($re_data);
}elseif($step == 2) {
    
    sleep(1);
    $re_data['error'] = false;
    echo json_encode($re_data);
    
}elseif($step == 3) {
    
    sleep(1);
    $re_data['error'] = false;
    echo json_encode($re_data);
    
}elseif($step == 4) {
    $re_data = array('success'=>'修改成功');
    echo json_encode($re_data);
}

?>
test.html
<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="/upload/bootstrap/bootstrap.min.css">  
        <script src="/upload/js/jquery.min.js"></script>
        <script src="/upload/bootstrap/bootstrap.min.js"></script>
        
        <style type="text/css">
            .form-group + .form-group { border-top: 1px solid #ededed; }
        </style>
        
    </head>
    <body>
        <div>
            <div>
                <input type="button" value="Upload" name="upload" id="button-upload">
            </div>
            <div>
                
                <div class="form-group">
                    <label class="col-sm-2 control-label">进度</label>
                    <div class="col-sm-10">
                      <div class="progress">
                        <div id="progress-bar" class="progress-bar" style="width: 1%;"></div>
                      </div>
                      <div id="progress-text"></div>
                    </div>
                </div>
                <div class="form-group">
            <label class="col-sm-2 control-label">将被覆盖的文件</label>
            <div class="col-sm-10">
              <textarea rows="10" readonly="" id="overwrite" class="form-control"></textarea>
              <br>
              <button type="button" id="button-continue" class="btn btn-primary" disabled=""><i class="fa fa-check"></i> Continue</button>
            </div>
          </div>
                
            </div>
            
        </div>
        <script>
            
            var step = new Array();
            var total = 0;
            $('#button-continue').on('click', function() {
                    next();
                    $('#button-continue').prop('disabled', true);
            });

            $('#button-upload').on('click', function() {
               
               $('#form-upload').remove();
               $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');
               $('#form-upload input[name=\'file\']').trigger('click');

                if (typeof timer != 'undefined') {
                    clearInterval(timer);
                }
                
                timer = setInterval(function() {
                    if ($('#form-upload input[name=\'file\']').val() != '') {
                            clearInterval(timer);

                            // Reset everything
                            $('#progress-bar').css('width', '0%');
                            $('#progress-bar').removeClass('progress-bar-danger progress-bar-success');
                            $('#progress-text').html('');

                            $.ajax({
                                    url: '/upload/test.php?step=1',
                                    type: 'post',
                                    dataType: 'json',
                                    data: new FormData($('#form-upload')[0]),
                                    cache: false,
                                    contentType: false,
                                    processData: false,
                                    beforeSend: function() {
                                            $('#button-upload').button('loading');
                                    },
                                    complete: function() {
                                            $('#button-upload').button('reset');
                                    },
                                    success: function(json) {
                                        if (json['error']) {
                                            $('#progress-bar').addClass('progress-bar-danger');
                                            $('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');
                                        }
                                        if (json['step']) {
                                            step = json['step'];
                                            total = step.length;

                                            if (json['overwrite'].length) {
                                                html = '';
                                                for (i = 0; i < json['overwrite'].length; i++) {
                                                    html += json['overwrite'][i] + "\n";
                                                }
                                                $('#overwrite').html(html);
                                                $('#button-continue').prop('disabled', false);
                                            } else {
                                                next();
                                            }
                                        }
                                    },
                                    error: function (xhr, ajaxOptions, thrownError) {
                                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                                    }
                                });
                            }
                        }, 500);
                });
                function next() {
                        data = step.shift();
                        if (data) {
                                $('#progress-bar').css('width', (100 - (step.length / total) * 100) + '%');
                                $('#progress-text').html('<span class="text-info">' + data['text'] + '</span>');
                                console.log(data.url);
                                $.ajax({
                                        url: data.url,
                                        type: 'post',
                                        dataType: 'json',
                                        data: data.param,
                                        success: function(json) { console.log(json);
                                                if (json['error']) {
                                                        $('#progress-bar').addClass('progress-bar-danger');
                                                        $('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');
                                                        $('#button-clear').prop('disabled', false);
                                                }

                                                if (json['success']) {
                                                        $('#progress-bar').addClass('progress-bar-success');
                                                        $('#progress-text').html('<span class="text-success">' + json['success'] + '</span>');
                                                }

                                                if (!json['error'] && !json['success']) {
                                                        next();
                                                }
                                        },
                                        error: function(xhr, ajaxOptions, thrownError) {
                                                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                                        }
                                });
                        }
                }

            
        </script>
        
    </body>
</html>



0 0