大文件合成
HTML提交文件
1 //HTML 2 3 <!DOCTYPE html> 4 <html lang="en"> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Document</title> 8 </head> 9 <body> 10 <input type="file" id="sc"> 11 </body> 12 </html> 13 <script> 14 document.getElementById('sc').onchange=function(){ 15 file=this.files[0]; 16 17 num=1; 18 start=0; 19 len=1024*1024; 20 end=start+len; 21 blob=cutfile(); 22 console.log(blob); 23 sendfile(); 24 } 25 26 function cutfile(){ 27 blob=file.slice(start,end); 28 start=end; 29 end=start+len; 30 return blob; 31 } 32 function sendfile(){ 33 xml=new FormData(); 34 xml.append("num",num); 35 xml.append("blob",blob); 36 xml.append("count",Math.ceil(file.size/len)); 37 ajax=new XMLHttpRequest(); 38 ajax.open("post","add.php",true); 39 ajax.send(xml); 40 if(start<file.size){ 41 t=setTimeout(function(){ 42 num++; 43 cutfile(); 44 sendfile(); 45 },1000); 46 }else{ 47 clearTimeout(t); 48 } 49 } 50 </script>
PHP合成文件
1 <?php 2 $file=$_FILES['blob']['tmp_name']; 3 $num=$_POST['num']; 4 $count=$_POST['count']; 5 move_uploaded_file($file,"a-".$num); 6 if($num==$count){ 7 $str=''; 8 for($i=1;$i<=$count;$i++){ 9 $str.=file_get_contents("a-".$i); 10 } 11 12 file_put_contents("test.mp4",$str); 13 } 14 ?>