java大视频上传实现
引入了两个概念:块(block)和片(chunk)。每个块由一到多个片组成,而一个资源则由一到多个块组成
将文件分割,分片上传,然后合并
var upstartBtn = document.getElementById('upstart');
var rate = document.getElementById('rate');
const LENGTH = 1024 * 1024 * 1;
var blob;
var file = null;
//-----------------------------
fileForm.onchange = function()
md5filename = md5; //如果需要刷新后也能断点,可利用cookie记录,自行完善
}
stopBtn.onclick = function(){
}
var xhr = new XMLHttpRequest();
file = that.files[0];
blob_num += 1;
this.stop = function(){
}
sendFile(blob,file);
var file_blob = file.slice(start,end);
return file_blob;
function sendFile(blob,file){
form_data.append('blob_num',blob_num);
form_data.append('file_name',file.name);
var progress;
progress = '100%';
}
rate.innerHTML = progress;
if(start < file.size && is_stop === 0){
blob_num += 1;
//setTimeout(t);
}
}
<?php
private $filepath = './upload'; //上传目录
private $totalBlobNum; //文件块总数
$this->blobNum = $blobNum;
$this->moveFile();
if($this->blobNum == $this->totalBlobNum){
$blob .= file_get_contents($this->filepath.'/'. $this->fileName.'__'.$i);
$this->deleteFileBlob();
for($i=1; $i<= $this->totalBlobNum; $i++){
}
private function moveFile(){
move_uploaded_file($this->tmpPath,$filename);
//API返回数据
if(file_exists($this->filepath.'/'. $this->fileName)){
$data['file_path'] = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['DOCUMENT_URI']).str_replace('.','',$this->filepath).'/'. $this->fileName;
if(file_exists($this->filepath.'/'. $this->fileName.'__'.$this->blobNum)){
$data['file_path'] = '';
header('Content-type: application/json');
if(!file_exists($this->filepath)){
}
return $md5FileName . '.' . pathinfo($fileName)['extension'];