我正在使用PECL uploadprogress,codeigniter方法(用于服务器端php脚本)和jQuery(用于调用服务器并计算百分比等)实现上传栏但是在调用运行getProgress的startProgress脚本时我有点问题回调php方法的循环中的脚本。 我看看谷歌Chrome控制台,看看以下内容
刚开始提交文件,post post 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);