項目結構圖:

14-slice-upload-fix.html檔案:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>大檔案切割上傳帶進度條</title>
<link rel="stylesheet" href="">
<script>
var xhr = new XMLHttpRequest();//xhr對象
var clock = null;
function selfile(){
clock = window.setInterval(sendfile,);
}
var sendfile = (function (){
const LENGTH = * * ;//每次上傳的大小
var start = ;//每次上傳的開始位元組
var end = start + LENGTH;//每次上傳的結尾位元組
var sending = false;//表示是否正在上傳
var fd = null;//建立表單資料對象
var blob = null;//二進制對象
var percent = ;
return (function (){
//如果有塊正在上傳,則不進行上傳
if(sending == true){
return;
}
var file = document.getElementsByName('video')[].files[];//檔案對象
//如果sta>file.size,就結束了
if(start > file.size){
clearInterval(clock);
return;
}
blob = file.slice(start,end);//根據長度截取每次需要上傳的資料
fd = new FormData();//每一次需要重新建立
fd.append('video',blob);//添加資料到fd對象中
up(fd);
//重新設定開始和結尾
start = end;
end = start + LENGTH;
sending = false;//上傳完了
//顯示進度條
percent = * start/file.size;
if(percent>){
percent = ;
}
document.getElementById('bar').style.width = percent + '%';
document.getElementById('bar').innerHTML = parseInt(percent)+'%';
});
})();
function up(fd){
xhr.open('POST','13-slice-upload.php',false);
xhr.send(fd);
}
</script>
<style>
#progress{
width:px;
height:px;
border:px solid green;
}
#bar{
width:%;
height:%;
background-color: green;
}
</style>
</head>
<body>
<h1>大檔案切割上傳帶進度條</h1>
<div id="progress">
<div id="bar"></div>
</div>
<input type="file" name="video" onchange="selfile();" />
</body>
</html>
13-slice-upload.php檔案:
<?php
/**
* 大檔案切割上傳,把每次上傳的資料合并成一個檔案
* @author webbc
*/
$filename = './upload/upload.wmv';//确定上傳的檔案名
//第一次上傳時沒有檔案,就建立檔案,此後上傳隻需要把資料追加到此檔案中
if(!file_exists($filename)){
move_uploaded_file($_FILES['video']['tmp_name'],$filename);
}else{
file_put_contents($filename,file_get_contents($_FILES['video']['tmp_name']),FILE_APPEND);
}
?>
運作結果圖: