ajaxFileUpload出问题了
本帖最后由 lirongsen3022 于 2013-02-27 10:06:57 编辑 在ie下运行时,文件能上传,但无法显示进度条的window(progress)。在firefox下运行时,非常正常。在chrome下运行时,问题就比较大了,ajaxFileUpload不向后台发送请求。用了jquery1.4.2.min.js,jquery.easyui.min.js,ajaxfileupload.js。高手们帮我看一下,谢谢!
function ajaxFileUpload(){
$('#progress').window('open');
//sleep(5000); //在ie下运行时,就算这里等待5秒也看不到progress弹出
//alert("1"); //在ie下运行时这里alert后能看到progress弹出,否则看不到progress弹出
//在$('#progress').window('open');和alert中间加入sleep的情况下,ie先sleep再两个对话框同时打开,这是为什么?
if($('#image').attr("value")!=""){
//在chrome下运行时,ajaxfileupload不发送请求
$.ajaxFileUpload(
{
url:'newsFileUpload.jsp?t='+new Date(), //需要链接到服务器地址
secureuri:false,
fileElementId:'image', //文件选择框的id属性
dataType: 'json', //服务器返回的格式,可以是json
success: function (data, status) //相当于java中try语句块的用法
{
//var json = JSON.parse(data)
if(data.success!=true){
$.messager.alert('注意',"数据处理出错,请重新上传",'warning');
}
},
error: function (data, status, e) //相当于java中catch语句块的用法
{
$.messager.alert('错误',"错误:"+e.description,'error');
}
}
);
var flag;
//读取上传进度
do{
$.ajax({
type:"POST",
async:false,
url:"newsUploadProgress.jsp?t=" +new Date(),
dataType:"json",
cache:false,
timeout:4000,
success:function(data){
//alert(data);
//var json = JSON.parse(data);
flag=data.percentage;
$('#p').progressbar('setValue', flag*100);
$('#message').html(data.msg);
}
});
}while(flag<1);
}else{
$.messager.alert('错误',"新闻列表文件不能为空",'error');
}
$('#progress').window('close');
$('#p').progressbar('setValue', 0);
return false;
}
function sleep(numberMillis) {
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}
ajaxfileupload easyui ajax
<div id="progress" class="easyui-window" data-options="closed:true,shadow:true,modal:true,collapsible:false,minimizable:false,maximizable:false,closable:false,resizable:false,draggable:false,title:'等待数据处理'" style="width:420px;height:130px;">
<div id="message" style="width:400px;margin:35px auto auto 2px"></div>
<div id="p" class="easyui-progressbar" style="width:400px;margin:1px auto auto 2px"></div>
</div>
function ajaxFileUpload() {
$('#progress').window('open');
if ($('#image').attr("value") != "") {
//在chrome下运行时,ajaxfileupload不发送请求
$.ajaxFileUpload(
{
url: 'newsFileUpload.jsp?t=' + new Date(), //需要链接到服务器地址
secureuri: false,
fileElementId: 'image', //文件选择框的id属性
dataType: 'json', //服务器返回的格式,可以是json
success: function (data, status) //相当于java中try语句块的用法
{
//var json = JSON.parse(data)
if (data.success != true) {
$.messager.alert('注意', "数据处理出错,请重新上传", 'warning');
}
},
error: function (data, status, e) //相当于java中catch语句块的用法
{
$.messager.alert('错误', "错误:" + e.description, 'error');
}
}
);
getPercentage();//循环独立成一个函数,可以重复调用
} else {
$.messager.alert('错误', "新闻列表文件不能为空", 'error');
$('#progress').window('close'); //////////
$('#p').progressbar('setValue', 0); ////////
}
/* $('#progress').window('close');
$('#p').progressbar('setValue', 0);*/
//这2句要放到2个分支中执行
return false;
}
function getPercentage() {
$.ajax({
type: "POST",
//async: false,
url: "newsUploadProgress.jsp?t=" + new Date(),
dataType: "json",
cache: false,
timeout: 4000,
success: function (data) {
//alert(data);
//var json = JSON.parse(data);
var flag = data.percentage;
$('#p').progressbar('setValue', flag * 100);
$('#message').html(data.msg);
if (flag < 1) getPercentage(); ////////
else {
$('#progress').window('close'); //////////
$('#p').progressbar('setValue', 0); ////////
}
}
});
}