登录  /  注册

html5大文件断点续传解决方法

小云云
发布: 2018-03-31 10:18:19
原创
2063人浏览过

本文主要和大家分享html5大文件断点续传解决方法,主要以代码的方法和大家分享,希望能帮助到大家。

js代码

!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8"/>  
        <title>xhr2</title>  
    </head>  
    <body>  
        <p  id="drop_area" style="border:3px dashed silver;width:200px; height:200px">  
            将图片拖拽到此  
        </p>  
          
        <progress value="0" max="10" id="prouploadfile"></progress>  
          
        <span id="persent">0%</span>  
        <br />  
        <!--<button onclick="xhr2()">ajax上传</button>-->     
        <button onclick="stopup()" id="stop">上传</button>      
        <script>  
        //拖拽上传开始  
        //-1.禁止浏览器打开文件行为  
        document.addEventListener("drop",function(e){  //拖离   
            e.preventDefault();      
        })  
        document.addEventListener("dragleave",function(e){  //拖后放   
            e.preventDefault();      
        })  
        document.addEventListener("dragenter",function(e){  //拖进  
            e.preventDefault();      
        })  
        document.addEventListener("dragover",function(e){  //拖来拖去    
            e.preventDefault();      
        })  
        //上传进度  
        var pro = document.getElementById(&#39;prouploadfile&#39;);  
        var persent = document.getElementById(&#39;persent&#39;);  
        function clearpro(){  
            pro.value=0;  
            persent.innerHTML="0%";  
        }  
          
        //2.拖拽  
        var stopbutton = document.getElementById(&#39;stop&#39;);  
          
        var resultfile=""  
        var box = document.getElementById(&#39;drop_area&#39;); //拖拽区域     
        box.addEventListener("drop",function(e){           
            var fileList = e.dataTransfer.files; //获取文件对象    
            console.log(fileList)  
            //检测是否是拖拽文件到页面的操作            
            if(fileList.length == 0){                
                return false;            
            }             
            //拖拉图片到浏览器,可以实现预览功能    
            //规定视频格式  
            //in_array  
            Array.prototype.S=String.fromCharCode(2);  
            Array.prototype.in_array=function(e){  
                var r=new RegExp(this.S+e+this.S);  
                return (r.test(this.S+this.join(this.S)+this.S));  
            };  
            var video_type=["video/mp4","video/ogg"];  
              
            //创建一个url连接,供src属性引用  
            var fileurl = window.URL.createObjectURL(fileList[0]);                
            if(fileList[0].type.indexOf(&#39;image&#39;) === 0){  //如果是图片  
                var str="<img     style="max-width:90%"+fileurl+"&#39; alt="html5大文件断点续传解决方法" >";  
                document.getElementById(&#39;drop_area&#39;).innerHTML=str;                   
            }else if(video_type.in_array(fileList[0].type)){   //如果是规定格式内的视频                    
                var str="<video width=&#39;200px&#39; height=&#39;200px&#39; controls=&#39;controls&#39; src=&#39;"+fileurl+"&#39;></video>";  
                document.getElementById(&#39;drop_area&#39;).innerHTML=str;        
            }else{ //其他格式,输出文件名  
                //alert("不预览");  
                var str="文件名字:"+fileList[0].name;  
                document.getElementById(&#39;drop_area&#39;).innerHTML=str;      
            }         
            resultfile = fileList[0];     
            console.log(resultfile);      
              
            //切片计算  
            filesize= resultfile.size;  
            setsize=500*1024;  
            filecount = filesize/setsize;  
            //console.log(filecount)  
            //定义进度条  
            pro.max=parseInt(Math.ceil(filecount));   
              
              
              
            i =getCookie(resultfile.name);  
            i = (i!=null && i!="")?parseInt(i):0  
              
            if(Math.floor(filecount)<i){  
                alert("已经完成");  
                pro.value=i+1;  
                persent.innerHTML="100%";  
              
            }else{  
                alert(i);  
                pro.value=i;  
                p=parseInt(i)*100/Math.ceil(filecount)  
                persent.innerHTML=parseInt(p)+"%";  
            }  
              
        },false);    
          
        //3.ajax上传  
  
        var stop=1;  
        function xhr2(){  
            if(stop==1){  
                return false;  
            }  
            if(resultfile==""){  
                alert("请选择文件")  
                return false;  
            }  
            i=getCookie(resultfile.name);  
            console.log(i)  
            i = (i!=null && i!="")?parseInt(i):0  
              
            if(Math.floor(filecount)<parseInt(i)){  
                alert("已经完成");  
                return false;  
            }else{  
                //alert(i)  
            }  
            var xhr = new XMLHttpRequest();//第一步  
            //新建一个FormData对象  
            var formData = new FormData(); //++++++++++  
            //追加文件数据  
              
            //改变进度条  
            pro.value=i+1;  
            p=parseInt(i+1)*100/Math.ceil(filecount)  
            persent.innerHTML=parseInt(p)+"%";  
            //进度条  
              
              
            if((filesize-i*setsize)>setsize){  
                blobfile= resultfile.slice(i*setsize,(i+1)*setsize);  
            }else{  
                blobfile= resultfile.slice(i*setsize,filesize);  
                formData.append(&#39;lastone&#39;, Math.floor(filecount));  
            }  
                formData.append(&#39;file&#39;, blobfile);  
                //return false;  
                formData.append(&#39;blobname&#39;, i); //++++++++++  
            formData.append(&#39;filename&#39;, resultfile.name); //++++++++++  
                //post方式  
                xhr.open(&#39;POST&#39;, &#39;xhr2.php&#39;); //第二步骤  
                //发送请求  
                xhr.send(formData);  //第三步骤  
                stopbutton.innerHTML = "暂停"  
                //ajax返回  
                xhr.onreadystatechange = function(){ //第四步  
                if ( xhr.readyState == 4 && xhr.status == 200 ) {  
                  console.log( xhr.responseText );  
                        if(i<filecount){  
                            xhr2();  
                        }else{  
                            i=0;  
                        }         
                }  
              };  
                //设置超时时间  
                xhr.timeout = 20000;  
                xhr.ontimeout = function(event){  
                alert(&#39;请求超时,网络拥堵!低于25K/s&#39;);  
              }           
                  
                i=i+1;  
                setCookie(resultfile.name,i,365)  
                  
        }  
          
        //设置cookie  
        function setCookie(c_name,value,expiredays)  
        {  
            var exdate=new Date()  
            exdate.setDate(exdate.getDate()+expiredays)  
            document.cookie=c_name+ "=" +escape(value)+  
            ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/")  
        }  
        //获取cookie  
        function getCookie(c_name)  
        {  
        if (document.cookie.length>0)  
          {  
          c_start=document.cookie.indexOf(c_name + "=")  
          if (c_start!=-1)  
            {   
            c_start=c_start + c_name.length+1   
            c_end=document.cookie.indexOf(";",c_start)  
            if (c_end==-1) c_end=document.cookie.length  
            return unescape(document.cookie.substring(c_start,c_end))  
            }   
          }  
        return ""  
        }  
          
          
        function stopup(){  
            if(stop==1){  
                stop = 0  
                  
                xhr2();  
            }else{  
                stop = 1  
                stopbutton.innerHTML = "继续"  
                  
            }  
              
        }  
        </script>  
    </body>  
</html>
登录后复制


php代码

    <?php  
    //$name=$_POST[&#39;username&#39;];  
    $dir=$_POST[&#39;filename&#39;];  
    $dir="uploads/".md5($dir);  
    file_exists($dir) or mkdir($dir,0777,true);  
      
      
    $path=$dir."/".$_POST[&#39;blobname&#39;];  
      
      
    //print_r($_FILES["file"]);  
    move_uploaded_file($_FILES["file"]["tmp_name"],$path);  
      
    if(isset($_POST[&#39;lastone&#39;])){  
        echo $_POST[&#39;lastone&#39;];  
        $count=$_POST[&#39;lastone&#39;];  
          
        $fp   = fopen($_POST[&#39;filename&#39;],"abw");  
        for($i=0;$i<=$count;$i++){  
            $handle = fopen($dir."/".$i,"rb");    
            fwrite($fp,fread($handle,filesize($dir."/".$i)));    
            fclose($handle);      
        }  
        fclose($fp);  
    }  
          
          
    ?>
登录后复制

相关推荐:

PHP如何实现文件下载断点续传

H5怎么实现文件断点续传

详解如何让PHP实现断点续传

以上就是html5大文件断点续传解决方法的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号