0

我正在使用 PECL uploadprogress、codeigniter 方法(用于服务器端 php 脚本)和 jQuery(用于调用服务器和计算百分比等)实现上传栏,但是当调用运行 getProgress 脚本的 startProgress 脚本时我有一点问题循环该回调 php 方法。我查看 google chrome 控制台并看到以下内容 刚开始发送文件

刚开始提交文件,调用php方法的帖子就被取消了,我找不到错误在哪里。显然,将循环制动到 json_get_uploadprogress 方法查询剩余字节。预先感谢您的帮助

表格:

<script type="text/javascript">
     $(document).ready(function(){
        $('#uploadform').submit(function() {
        var uid = $("#uid").val();
        startProgress(uid);
     });
 });

 <div class="forms" style="margin:20px;clear: left">
     <form id="uploadform" action="/miadmin/upload_foto" enctype="multipart/form-data" method="post" class="forms">      
                <input type="hidden" id="uid" name="UPLOAD_IDENTIFIER" value="<?php echo $unique_id ?>" id="progress_key" />
                <input type="hidden" name="cliente" value="<?php echo $dir_id ?>" />                  
                <input type="file" value="Selecciona una foto" name="foto" id="foto" style="float:left;width:400px" /><br>
                <input type="submit" value="Agregar" id="agregar" style="float:left" />                   
            </form>
            <div id="progressBar" class="progressbar"></div> 
            <iframe id="uploadIframe" name="uploadIframe" src=""></iframe>
            <script type="text/javascript">var url = "<?php echo base_url();?>";</script>
            <script type="text/javascript" src="<?php echo base_url();?>js/uploadprogress.js"></script>
         <?php }  ?>
    </div>           

调用 php 的 Javascript 代码:

function getProgress(uploadKey){
$.post("json_get_uploadprogress",{"upload_id":uploadKey},
function(data)
{
   if(!data) return;
   var result = data.result;
   var percentage = 0;
   percentage = data.percentage;
   if (percentage > 0) {
   $("#progressBar").html("<h3>Upload progress...</h3><div  cass='progressGrey'>"+result+"    <div class='progressRed' style='width:"+percentage+"%'>"+result+"</div></div>");
   } else {
   $("#progressBar").html("<h3>Upload progress...</h3><div xlass='progressGrey'>"+result+"</div>");
}
if (percentage < 100) {
  var timeoutID = window.setTimeout("getProgress(theUploadKey)", 1000); //query every 1 second
}
if(percentage >= 100)
{
    $("iframe#uploadIframe").css('display','block');
    var body = $("iframe#uploadIframe").contents().find("body");
    //Remaquetamos el body del iframe por culpa de IE
    body.css('border','0px none');
    body.css('background-color',$("body").css('background-color'));
    body.css('text-align','center');
}
}, "json"
);
}

function startProgress(uploadKey) {
theUploadKey = uploadKey;
 $("#progressBar").css('display','block');
 $("#progressBar").html("<h3>Upload progress...</h3><div class='progressGrey'> </div>");
  $("form#uploadform").css('display','none');
  getProgress(uploadKey);
return null;
}

和php方法:

public function json_get_uploadprogress()
 {
 $data = array();
 if($this->input->post('upload_id'))
 {
     $uploadKey = $this->input->post('upload_id');
     $status = false;
     $percentage = 0;
     $result = "INITIALISING UPLOAD";
     $data["percentage"] = $percentage;
     $data["result"] = $result;
     if (function_exists('uploadprogress_get_info'))
     {
        $status = uploadprogress_get_info($this->input->get('uid'));
     }
     if (is_array($status))
     {
         log_message("error", "STATUS: ".print_r($status,TRUE));
             if (array_key_exists("bytes_total", $status) && array_key_exists("bytes_uploaded", $status))
             {
             $percentage = round((($status['bytes_uploaded'] / $status['bytes_total']) * 100),2);
                 if ($status['bytes_uploaded'] >= $status['bytes_total'])
                 {
                 $percentage = 100;
                 $result = "UPLOAD COMPLETE";
                 }
                 else
                 {
                  $bytes = array('B','KB','MB','GB','TB');
                  foreach($bytes as $val)
                  {
                    if($status["bytes_total"]  > 1024)
                    {
                    $status["bytes_total"] = $status["bytes_total"] / 1024;
                    $status["bytes_uploaded"] = $status["bytes_uploaded"] / 1024;
                    }
                    else
                    {
                    break;
                    }
                  }
                $result =  $percentage."% (".round($status["bytes_uploaded"], 2)." ";
                $result.= $val." of ".round($status["bytes_total"], 2)." ".$val.")";
                }
            $data["percentage"] = $percentage;
            $data["result"] = $result;
            }

    }
}
$this->load->view('miadmin/json_response_view',array("array" => $data));
} 

json_response_view.php 视图只包含 json 格式的数据响应:

echo json_encode($array); 
4

0 回答 0