• js分割文件快速上传


    <?php header('Content-type:text/html;charset=UTF-8'); ?>
    
    <?php
    if ($_FILES) {
      
      if(!file_exists('./uploads/123.zip')) {
        move_uploaded_file($_FILES['part']['tmp_name'],'./uploads/123.zip');
      } else {
        file_put_contents('./uploads/123.zip',file_get_contents($_FILES['part']['tmp_name']),FILE_APPEND);
        unlink($_FILES['part']['tmp_name']);
      }
    
      echo 'ok';
      exit;
    }
    ?>
    
    <h1>html5大文件切割上传</h1>
    <div id="bar" style="">
        <span id="progress">0%</span>
    </div>
    
    <input name="mov" type="file" />
    <input id="btn" type="button" value="点我" />
    
    <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $('#btn').click(function() {
                sendfile();
            });
        })
    
         function sendfile() {
             const LENGTH = 1024 * 1024;
             var sta = 0;
             var end = sta + LENGTH;
             var blob = null;
             var fd = null;
    
             /*
                 xhr 对象
             */
             var xhr = null;
    
             var mov = document.getElementsByName('mov')[0].files[0];
             //console.log(mov);return;
    
             var totalsize = mov.size;
             var percent = 0;
    
             // while(sta < totalsize) {
             timer = setInterval(function(){
                if (sta>totalsize) {
                    clearInterval(timer);
                };
                blob = mov.slice(sta,end);
                 fd = new FormData();
                 fd.append('part',blob);
    
                 xhr = new XMLHttpRequest();
                 xhr.open('POST',"",false);
    
                 xhr.send(fd);
    
                 sta = end;
                 end = sta + LENGTH; 
    
                 percent = 100 * end / totalsize;
                 if(percent > 100) {
                     percent = 100;
                 }
                 // document.getElementById('bar').style.width = percent + '%';
                // $('#bar').width(percent+'%');
                 $('#bar').css({'width':percent+'%', 'background-color':'red'});
                 $('#progress').html(parseInt(percent)+'%');
             },1)
    
             // }
    
         }
    
    
    </script>
    

      

  • 相关阅读:
    RedHat5.8 编译内核驱动 合成initrd.img
    matrix-gui-2.0 将javascript文件夹改成js文件夹
    使用PHP配置文件
    Ubuntu 16.10 Apache PHP Server
    Ubuntu 16.10 中文环境 Shell输出英文提示
    制作SD卡img文件,并扩容
    Linux syslogd
    Windows cmd findstr
    jquery ztree异步搜索
    怎样在点击li时添加样式,移除兄弟样式
  • 原文地址:https://www.cnblogs.com/adtuu/p/4739538.html
Copyright © 2020-2023  润新知