Ajax大檔案切割上傳

來源:互聯網
上載者:User

標籤: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大檔案切割上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.