【发布时间】:2013-07-24 04:35:17
【问题描述】:
嗨,我实际上正在使用这个插件https://github.com/englercj/jquery-ajax-progress/
它只是在 ajax 请求中添加了一个 progress: 语句。
效果很好,我的代码如下所示:
var _min_width = 470;
var _min_height = 330;
var _which;
var _fyle_type;
var _file_name;
var allowed_types = new Array('image/png','image/jpg','image/jpeg');
if (typeof(FileReader) === 'function'){
$('input[type="file"]').on('change', function(e) {
_file_name = $(this).val();
var file = e.target.files[0];
if (!in_array(file.type,allowed_types) || file.length === 0){
notify("You must select a valid image file!",false,false);
return;
}
if(file.size > 3145728 /*3MB*/){
notify("<?php echo lang('each-photo-1MB'); ?>",false,false);
return;
}
notify_destroy();
var reader = new FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(file);
});
function fileOnload(e) {
var img = document.createElement('img');
img.src = e.target.result;
img.addEventListener('load', function() {
if(img.width < _min_width || img.height < _min_height ){
notify("<?php echo lang('each-photo-1MB'); ?>",false,false);
return;
}
//remove not-needed base64 data:pfff
var clear_string = e.target.result.replace('data:image/jpeg;base64,','').replace('data:image/png;base64,','');
var _data;
if(_which == 'photo_1'){
_data = {photo_1:clear_string};
}if(_which == 'photo_2'){
_data = {photo_2:clear_string};
}if(_which == 'photo_3'){
_data = {photo_3:clear_string};
}if(_which == 'photo_4'){
_data = {photo_4:clear_string};
}
$.ajax({
type:'post',
dataType:'text',
data:_data,
url:_config_base_url+'/upload/upload_photos',
beforeSend:function(){
$('.'+_which+'_holder').fadeOut(0);
$('.'+_which+'_progress').fadeIn(0);
$('.'+_which+'_progress').addClass('progress-striped');
},
progress:function(e){
//make sure we can compute the length
if(e.lengthComputable) {
//calculate the percentage loaded
var pct = (e.loaded / e.total) * 100;
$('.'+_which+'_progress .bar').css({'width':pct+'%'});
$('.'+_which+'_progress .progress-opt span').text(pct.toFixed(0));
//console.log(pct);
}else {
console.warn('Content Length not reported!');
}
},
success:function(){
alert(_file_name+' uploaded ok');
},
complete:function(){
$('.'+_which+'_progress .progress-opt span').text('100');
$('.'+_which+'_holder p').text(_file_name);
$('.'+_which+'_progress .bar').css({'width':'100%'});
$('.'+_which+'_progress').delay(300).removeClass('progress-striped').fadeOut(0,function(){
$('.'+_which+'_holder').show(0);
$('.'+_which+'_progress .bar').css({'width':'0'});
});
}
});
});
}
}
现在的问题是,当我在 progress: ajax 语句中设置 text(pct.toFixed(0)) 时,我需要使 pct “实时”,我的意思是,这段代码以百分比表示请求进度,所以你可以看到百分比它需要请求才能结束。
问题是,如果我启动其中 2 个请求,pct 将被共享 两个请求,所以如果 request 1 处于 40% progress (pct) 那么 request 2 将从request 1
继承相同的进度百分比(pct = 40%)
为了更好地解释我想发起许多这样的请求,但为了> 保持每个请求的进度百分比(var pct)唯一
【问题讨论】:
-
问题是
which有相同的值或者两组ajax进度调用。如果你得到whichinfo 一个闭包对于每个调用都是唯一的,它应该可以工作。最后打印出来的file_name和最后的进度更新也会遇到同样的问题。 -
@LeeMeador
_which在每个change()上更改他的值,所以在随后启动的每个请求中,但我不能这样做 var _which.pct !? -
@LeeMeador 不,伙计,每个请求的_file_name 总是新的,检查更好我已经测试过并且它有效,只是问题是使 pct 不共享:P
-
@LeeMeador 对不起我的错误我粘贴了整个 js 现在很清楚我认为..
标签: javascript jquery progress percentage