"/> xhr2 ">

Home >Backend Development >PHP Tutorial >HTML5 solves the problem of resuming upload of large files after breakpoints

HTML5 solves the problem of resuming upload of large files after breakpoints

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-28 08:26:561038browse

js code

[html] view plain copy html5解决大文件断点续传 html5解决大文件断点续传

  1. >
  2. <html>
  3. <head>
  4.                                                                                                                                                                                                                                                                                                                     title>
  5. head
  6. > <body> <
  7. div id="drop_area"
  8. style="border: 3px dashed silver;width:200px; height:200px" >
  9. Drag the image here div>                                                                                                        value="0" max
  10. =
  11. "10"
  12. id="prouploadfile">
  13. progress
  14. >
  15. ent">0%span >           <button onclick
  16. =
  17. "stopup()"
  18. id="stop" >Upload button>                                                                                                                                         ​/Drag and drop upload starts                                                                                                                  . " })                                                                                                                                                                            There not worse not No matter more                         in the other’s being’ themselves —    (e){ //Drag and drop                                                                                                    )
  19. document.addeventListener ("Dragenter", function (e) {// Drags
  20. e.preventdeFault ();
  21. })
  22. docume {
  23. var pro
  24. =
  25. document
  26. . getElementById('prouploadfile');                                                       function clearpro(){
  27. pro.value =0;
  28.                                                                                                            
  29. var stopbutton = document.getElementById('stop');
  30.                                                                                            var box
  31. = document.getElementById('drop_area'); //Drag area box.addEventListener("drop",function(e){
  32. var fileList = e.dataTransfer.files; //Get the file object
  33. console.log(fileList)
  34. //Detect whether the file is dragged to the page                                                                                             .length
  35. == 0){                                                                                   到 // Drag the picture to the browser, you can realize the preview function
  36. // Standardized video format
  37. // in_array
  38. Array.prototype.s
  39. = String .fromCharCode(2); ​​ var r
  40. =new RegExp(this.S +e+this.S);                                                    var video_type=["video/mp4","video/ogg"];
  41.                                                                                                                                                                                              var fileurl = window.URL.createObjectURL(fileList[0]);          var str
  42. =">
  43. "; document.getElementById('drop_area ').innerHTML
  44. =str
  45. ; ’s video                                     str
  46. =
  47. "
  48. "; T document.GetelementByid ('Drop_area') .innerhtml
  49. =
  50. str; } else {// Other formats, output file names // alert ("" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" Alert ("" "" " No preview");
  51. var str="File name: "+fileList[0].name;
  52. document.getElementById('drop_area').innerHTML
  53. =
  54. str; }
  55.                                                                                                                                                                                                                                                                                                                                                             = resultfile.size;
  56.                                                                      file                                                             //console.log(filecount)
  57.                                                              ;                                                
  58.                                                                                                                                                                      
  59.                                                                                                                                                                          i):0                                                                                                                                                                                                 . .value=i+1;
  60.                                                                                                        alert (i); =parseInt
  61. (i)*100/Math.ceil(filecount)                                                                                                                                                                                                                                                                                                           ,false);
  62. =
  63. 1;
  64. O if (Stopf == 1) { Return false;
  65. }
  66. if (ResultFile == "") {
  67. Alert ("" Please select a file")
  68.                                                                                                                                                                                                                                                                 
  69. getCookie(resultfile.name);
  70. ​​​​If(Math.floor(filecount)
  71. <parseInt
  72. (i)){
  73. alert("Completed");
  74. return false;                                                                                                           
  75.                                                                                                                                                         var formData = new
  76. FormData(); //++++++++++
  77.                                                                                                                                                                                                                                         i+1;
  78.                                                                                                                                                                                                                                             ? parseInt
  79. (p)+"%";
  80. //Progress bar                                                                                                                                                                                                                                                                                                                                                                                ​                                                                                               resultfile
  81. .slice(i*setsize,filesize);​​​​formData.append('blobname', i); //++++++++++ +
  82.            formData.append('filename', resultfile.name); //++++++++++                                                                                                                     formData.append('filename', resultfile.name);       //++++++++++                                                  xhr.open('POST', 'xhr2.php'); //The second step
  83.                                            ​​​​​​​​stopbutton.innerHTML
  84. = " Pause "
  85.                                                                                                                                                                                      
  86. (){ //Step 4
  87.                                                         == 4 && xhr.status
  88. == 200 ) {
  89.                   console.log(xhr.responseText); If(i<filecount){
  90.                                                                                                                                                                                                                                                                                                        /Set timeout
  91.                                             
  92. xhr.ontimeout = function(event){
  93. Request timed out, network congestion!低于25K/s');  
  94.               }           
  95.                   
  96.                 i=i+1;  
  97.                 setCookie(resultfile.name,i,365)  
  98.                   
  99.         }  
  100.           
  101.         //设置cookie  
  102.         function setCookie(c_name,value,expiredays)  
  103.         {  
  104.             var exdate=new Date()  
  105.             exdate.setDate(exdate.getDate()+expiredays)  
  106.             document.cookie=c_name+ "=" +escape(value)+  
  107.             ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/")  
  108.         }  
  109.         //获取cookie  
  110.         function getCookie(c_name)  
  111.         {  
  112.         if (document.cookie.length>0)  
  113.           {  
  114.           c_start=document.cookie.indexOf(c_name + "=")  
  115.           if (c_start!=-1)  
  116.             {   
  117.             c_start=c_start + c_name.length+1   
  118.             c_end=document.cookie.indexOf(";",c_start)  
  119.             if (c_end==-1) c_end=document.cookie.length  
  120.             return unescape(document.cookie.substring(c_start,c_end))  
  121.             }   
  122.           }  
  123.         return ""  
  124.         }  
  125.           
  126.           
  127.         function stopup(){  
  128.             if(stop==1){  
  129.                 stop = 0  
  130.                   
  131.                 xhr2();  
  132.             }else{  
  133.                 stop = 1  
  134.                 stopbutton.innerHTML = "继续"  
  135.                   
  136.             }  
  137.               
  138.         }  
  139.         script>  
  140.     body>  
  141. html>  

php代码

[php] view plain copy html5解决大文件断点续传 html5解决大文件断点续传

  1. //$name=$_POST['username'];  
  2. $dir=$_POST['filename'];  
  3. $dir="uploads/".md5($dir);  
  4. file_exists($diror mkdir($dir,0777,true);  
  5.   
  6.   
  7. $path=$dir."/".$_POST['blobname'];  
  8.   
  9.   
  10. //print_r($_FILES["file"]);  
  11. move_uploaded_file($_FILES["file"]["tmp_name"],$path);  
  12.   
  13. if(isset($_POST['lastone'])){  
  14.     echo $_POST['lastone'];  
  15.     $count=$_POST['lastone'];  
  16.       
  17.     $fp   = fopen($_POST['filename'],"abw");  
  18.     for($i=0;$i<=$count;$i++){  
  19.         $handle = fopen($dir."/".$i,"rb");    
  20.         fwrite($fp,fread($handle,filesize($dir."/".$i)));    
  21.         fclose($handle);      
  22.     }  
  23.     fclose($fp);  
  24. }  
  25.       
  26.       
  27. ?> 

以上就介绍了 html5解决大文件断点续传,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn