日期:2014-05-16 浏览次数:20435 次
(function(){
function addEvent( node,type,listener) {
if (node.addEventListener) {
node.addEventListener( type, listener, false );
return true;
} else if(node.attachEvent) {
node['e'+type+listener] = listener;
node[type+listener] = function(){node['e'+type+listener]( window.event );}
node.attachEvent( 'on'+type, node[type+listener] );
return true;
}
return false;
};
var submit=document.forms["myform"];
addEvent(submit,'submit',startUpload);
var begin;
var request;
var rdm;
var pec=document.getElementById("pecent");
function startUpload()
{
rdm=Math.floor(Math.random()*100000000);
document.getElementById('remark').setAttribute('value',rdm);
document.getElementById("progress").style['display']='block';
//creatXmlHttpRequest();
begin=setTimeout(doRequest,1000);
};
function creatXmlHttpRequest()
{
if(window.ActiveXObject)
{request=new ActiveXObject("Microsoft.XMLHTTP")}
else{request=new XMLHttpRequest();}
};
var count=0;
function doRequest()
{
if(window.ActiveXObject)
{request=new ActiveXObject("Microsoft.XMLHTTP");}
else{request=new XMLHttpRequest();}
if(request!=null){
request.onreadystatechange=handle;
request.open("GET","upload.php?key="+rdm+"&sim=" + (++count),true);
request.send();
}
};
function handle(){
if(request.readyState==4&&request.status==200)
{
//接受服务器数据
var pec=document.getElementById("pecent");
var prgs=eval("(" +request.responseText + ")");
var prgs=request.responseText;
var cur=parseInt(prgs.current);
var total=parseInt(prgs.total);
var pecentIs=Math.round(cur/total*100);
pec.innerHTML=pecentIs.toString()+"%";
if(100==pecentIs)
{
document.getElementById("rst").innerHTML="上传成功";
pec.style['width']="200px";
clearTimeout(begin);
}else{
begin=setTimeout(doRequest,1000);
alert(pecentIs);
pec.style['width']=pecentIs*2;
}
}
};
})();