您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

JS上传文件时显示进度条

2024/4/27 14:06:08发布4次查看
这次给大家带来js上传文件时显示进度条,js上传文件时显示进度条的注意事项有哪些,下面就是实战案例,一起来看一下。
在php.ini修改需要的大小:
upload_max_filesize = 8m
post_max_size = 10m
memory_limit = 20m
<!doctype html> <html> <head>   <title>原生js大文件显示进度条</title>   <meta charset="utf-8">   <style type="text/css">     #parent{position: relative;width: 500px;height:20px;border:1px solid #ccc;display: none;border-radius:20px}     #child{position: absolute;width:0%;height:20px;background: #5fb878;display: none;line-height: 20px;color: #ffffff;font-size: 12px;border-radius:20px}   </style>   <script type="text/javascript">     function $(id){       return document.getelementbyid(id);     }   </script> </head> <body>   <form action="" method="post">     <p id="parent">       <p id="child"></p>     </p>     <p>上传文件:<input type="file" name="file"></p>       <p><input type="submit" value="提交" id="submit"></p>   </form>   <script type="text/javascript">     var oform = document.getelementsbytagname('form')[0];     var osubmit = $('submit');     //如果多个人同时提交这个表单的时候,由于是异步的请求,互不影响     osubmit.onclick = function(){       try{         var xhr = new xmlhttprequest();       }catch(e){         var xhr = new activexobject(msxml2.xmlhttp);       }       xhr.upload.onprogress = function(e){         var ev = e || window.event;         var percent = math.floor((ev.loaded / ev.total)*100);             // console.log(percent);         //将百分比显示到进度条         $('parent').style.display = 'block';         $('child').style.display = 'block';         //将上传进度的百分比显示到child里面         $('child').style.width = percent+'%';         $('child').style.textalign = 'center';         $('child').innerhtml = percent+'%';         //判断如果百分比到达100%时候,隐藏掉         if(percent==100){           $('parent').style.display = 'none';           $('child').style.display = 'none';         }       }       xhr.open('post','progress.php',true);       var form = new formdata(oform);       xhr.send(form);       xhr.onreadystatechange = function(){         if(xhr.readystate==4 && xhr.status==200){           eval(var obj =+xhr.responsetext);           if(obj.status){             alert('上传成功');           }else{             alert('上传失败');           }         }       }       //阻止表单提交       return false;     }   </script> </body> </html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
以上就是js上传文件时显示进度条的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录