• Ajax上传文件进度条显示


    要实现进度条的显示,就要知道两个参数,上传的大小和总文件的大小
    html5提供了一个上传过程事件,在上传过程中不断触发,然后用已上传的大
    小/总大小,计算上传的百分比,然后用这个百分比控制div框的显示,就可以
    实现上传的进度条效果
    


    前端页面

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Ajax上传文件进度条显示</title>
    <script type="text/javascript">
    function upfile(){
    	var pic=document.getElementsByTagName('input')[0].files[0];
    	var fd=new FormData();
    	var xhr=new XMLHttpRequest();
    	xhr.open('post','01.php',true);
    	xhr.onreadystatechange=function (){
    		if(this.readyState==4){
    			document.getElementById('precent').innerHTML=this.responseText;
    		}
    	}
    	xhr.upload.onprogress=function (ev){
    		//console.log(ev);控制台打印progress { target: XMLHttpRequestUpload, isTrusted: true, lengthComputable: true,
    //loaded: 15020, total: 15020, eventPhase: 0, bubbles: false, cancelable: false, defaultPrevented: false,
    //timeStamp: 1445144855459000, originalTarget: XMLHttpRequestUpload } if(ev.lengthComputable){ var precent=100 * ev.loaded/ev.total; console.log(precent); document.getElementById('nei').style.width=precent+'%'; document.getElementById('precent').innerHTML=Math.floor(precent)+'%'; } } fd.append('pic',pic); xhr.send(fd); } </script> <style> #wai{ 500px; height:30px; border:1px solid green; } #nei{ 0px; height:30px; background:green; } </style> </head> <body> <div id="wai"> <div id="nei"></div> </div><span id="precent"></span><br/> <input type="file" name="pic" onchange="upfile();"/> </body> </html>

     页面显示效果

  • 相关阅读:
    Hadoop学习笔记—20.网站日志分析项目案例(二)数据清洗
    python四舍五入保留2位小数
    查看python中的keywords(关键字)和modules
    c:forEach实现动态select标签
    解决Windows上数据库密码忘记问题
    Java读取properties文件总结
    URL地址最大长度问题
    Servlet生命周期
    解决mysql 数据库连接密码
    Java中int与integer的区别
  • 原文地址:https://www.cnblogs.com/lzzhuany/p/4889413.html
Copyright © 2020-2023  润新知