標籤:style blog http io ar color os sp java
1問:在不更改php.ini中post_max_size的情況下怎麼實現大檔案的上?
答把大檔案切割成許多小塊,分塊上傳,傳完後,重新合并成大檔案即可。
2問:怎麼切割?
答:用html5中的file API,這個API繼承自Blob對象,Blob中有slice方法,可以截取二進位對象的一部分,實現切割大檔案的效果。
3問:具體思路呢?
答:截取10M上傳,判斷是否截取完畢
while 還有資料{ 截取, Ajax上傳 }
1 <script type="text/javascript"> 2 unction sendfile() { 3 const LENGTH = 10 * 1024 * 1024; 4 var sta = 0; 5 var end = sta + LENGTH; 6 var blob = null; 7 var fd = null; 8 9 /*10 xhr 對象11 */12 var xhr = null;13 14 var mov = document.getElementsByName(‘mov‘)[0].files[0];15 //console.log(mov);return;16 17 var totalsize = mov.size;18 var percent = 0;19 20 while(sta < totalsize) {21 blob = mov.slice(sta,end);22 fd = new FormData();23 fd.append(‘part‘,blob);24 25 xhr = new XMLHttpRequest();26 xhr.open(‘POST‘,‘06-sliceup.php‘,false);27 28 xhr.send(fd);29 30 sta = end;31 end = sta + LENGTH; 32 33 percent = 100 * end / totalsize;34 if(percent > 100) {35 percent = 100;36 }37 document.getElementById(‘bar‘).style.width = percent + ‘%‘;38 document.getElementById(‘bar‘).innerHTML = parseInt(percent) + ‘%‘;39 }40 41 }42 43 44 </script>
html頁面
</head> <body> <h1>html5大檔案切割上傳</h1> <div id="progress"> <div id="bar"></div> </div> <input type="file" name="mov" onchange="sendfile();" /> </body></html>
後台要有個php頁面,把檔案接收併合並起來
1 if(!file_exists(‘./upload/hhr.mov‘)) {2 move_uploaded_file($_FILES[‘part‘][‘tmp_name‘],‘./upload/hhr.mov‘);3 } else {4 file_put_contents(‘./upload/hhr.mov‘,file_get_contents($_FILES[‘part‘][‘tmp_name‘]),FILE_APPEND);5 }6 7 echo ‘ok‘;
Ajax大檔案切割上傳