1
파일을 업로드하고 html5 청킹을 사용하여 파일을 조각 내기위한 코드가 있습니다. 현재 진행률 막대는 파일의 모든 덩어리에 대한 것이므로 하나의 진행률 표시 줄로 만들려면 어떻게해야합니까? 모든 덩어리에 대한 응답의 경우에는 어떻게해야합니까? 예를 들어, 예제 코드 및 사진 : 각 진행률 표시 줄 만 등 한 바 있지만 아직 정확한 비율을 가지고에 그 진행 표시 줄을 결합하여 하나의 파일의 덩어리 (1메가바이트)하지만 방법에 해당청크 파일 용 HTML5 진행률 막대
var uploaders = [];
var totalChunks = 0;
var i = 0;
$(document).ready(function() {
var progress = document.querySelector('progress');
var bars = document.querySelector('#bars');
});
//function for after the button is clicked, slice the file
//and call upload function
function sendRequest() {
//clean the screen
//bars.innerHTML = '';
var blob = document.getElementById('fileToUpload').files[0];
var originalFileName = blob.name;
const BYTES_PER_CHUNK = 1 * 1024 * 1024; // 10MB chunk sizes.
const SIZE = blob.size;
var start = 0;
var end = BYTES_PER_CHUNK;
totalChunks = Math.ceil(SIZE/BYTES_PER_CHUNK);
while(start < SIZE) {
if (blob.webkitSlice) {
var chunk = blob.webkitSlice(start, end);
} else if (blob.mozSlice) {
var chunk = blob.mozSlice(start, end);
}
uploadFile(chunk, originalFileName, totalChunks);
start = end;
end = start + BYTES_PER_CHUNK;
}
}
function uploadFile(blobFile, fileName, totalChunks) {
var progress = document.createElement('progress');
progress.min = 0;
progress.max = 100;
progress.value = 0;
bars.appendChild(progress);
var fd = new FormData();
fd.append("fileToUpload", blobFile);
var xhr = new XMLHttpRequest();
xhr.open("POST", "upload.php"+"?"+"file="+fileName + i, true);
i++;
xhr.onload = function(e) {
//make sure if finish progress bar at 100%
progress.value = 100;
//counter if everything is done using stack
uploaders.pop();
if (!uploaders.length) {
bars.appendChild(document.createElement('br'));
bars.appendChild(document.createTextNode('DONE :)'));
mergeFile(fileName, totalChunks);
}
};
// Listen to the upload progress for each upload.
xhr.upload.onprogress = function(e) {;
if (e.lengthComputable) {
progress.value = (e.loaded/e.total) * 100;
}
};
uploaders.push(xhr);
xhr.send(fd);
}