zoukankan      html  css  js  c++  java
  • js获取文件上传进度

    js获取文件上传进度:

    <input name="file" id="FileUpload" type="file" />
    <input type="button" onclick="Submit()" value="提交" />
    

    js监听:

    var xhrOnProgress=function(fun) {
    	  xhrOnProgress.onprogress = fun; //绑定监听
    	  //使用闭包实现监听绑
    	  return function() {
    	    //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
    	    var xhr = $.ajaxSettings.xhr();
    	    //判断监听函数是否为函数
    	    if (typeof xhrOnProgress.onprogress !== 'function')
    	      return xhr;
    	    //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
    	    if (xhrOnProgress.onprogress && xhr.upload) {
    	      xhr.upload.onprogress = xhrOnProgress.onprogress;
    	    }
    	    return xhr;
    	  }
    }
    

     ajax文件上传函数:

    function Submit(){
    
    		 var fileObj = document.getElementById("FileUpload").files[0]; // js 获取文件对象
    		 var formFile = new FormData();
    		 
             formFile.append("file", fileObj); //加入文件对象
            
              var data = formFile;
              $.ajax({
                       url: url,
                       data: data,
                       type: "Post",
                       dataType: "json",
                       cache: false,//上传文件无需缓存
                       processData: false,//用于对data参数进行序列化处理 这里必须false
                       contentType: false, //必须
                       xhr:xhrOnProgress(function(e){
    		   			  var percent=e.loaded/e.total;//文件上传百分比
    		   			  console.log(percent);
    		   		   }),
                       success: function (result) {
                           console.log(result);
                       },
                   })
    	}
    

      

    完整代码:

    <!DOCTYPE html>
    <html lang="zh">
    <head>
    	<meta charset="UTF-8" />
    	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
    	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
    	<title>Document</title>
    </head>
    <body>
    	
    	  <input name="file" id="FileUpload" type="file" />
    
    	  <input type="button" onclick="Submit()" value="提交" />
    
    </body>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript">
    	var xhrOnProgress=function(fun) {
    	  xhrOnProgress.onprogress = fun; //绑定监听
    	  //使用闭包实现监听绑
    	  return function() {
    	    //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
    	    var xhr = $.ajaxSettings.xhr();
    	    //判断监听函数是否为函数
    	    if (typeof xhrOnProgress.onprogress !== 'function')
    	      return xhr;
    	    //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
    	    if (xhrOnProgress.onprogress && xhr.upload) {
    	      xhr.upload.onprogress = xhrOnProgress.onprogress;
    	    }
    	    return xhr;
    	  }
        }
    	
    	function Submit(){
    
    		 var fileObj = document.getElementById("FileUpload").files[0]; // js 获取文件对象
    		 var formFile = new FormData();
    		
             formFile.append("file", fileObj); //加入文件对象
            
              var data = formFile;
              $.ajax({
                       url: "http://up.qiniu.com/",
                       data: data,
                       type: "Post",
                       dataType: "json",
                       cache: false,//上传文件无需缓存
                       processData: false,//用于对data参数进行序列化处理 这里必须false
                       contentType: false, //必须
                       xhr:xhrOnProgress(function(e){
    		   			  var percent=e.loaded/e.total;
    		   			  console.log(percent);
    		   		   }),
                       success: function (result) {
                           console.log(result);
                       },
                   })
    	}
    	
    </script>
    </html>
    

      

  • 相关阅读:
    (二)php的常量和变量
    关于标签系统的一点想法。
    Linux运维工程师中级面试题
    Linux C 编程内存泄露检测工具(一):mtrace
    掌握sudo的使用
    Scala极速入门
    处理千万级以上的数据提高查询速度的方法
    linux svn服务器搭建、客户端操作、备份与恢复
    select/poll/epoll 对比
    汇编指令和标志寄存器
  • 原文地址:https://www.cnblogs.com/cyrfr/p/9430730.html
Copyright © 2011-2022 走看看