Sử dụng tính năng chunk HTML5, tôi có thể tải tệp lên bằng phần nhỏ hơn. Nhưng vấn đề bắt đầu khi nó bắt đầu sử dụng nhiều yêu cầu http POST, điều này sẽ làm máy tính bị chậm lại hoặc có thể gặp sự cố. Có anyway để có các tập tin chia nhỏ theo một yêu cầu http .. vì vậy nếu tôi có 5 tập tin nó sẽ chỉ có 5 http yêu cầu eventhough tôi sử dụng html5 chia chunkCách tải lên nhiều tệp dưới 1 yêu cầu http
ví dụ: nếu tôi tải lên 5 tệp, mỗi tệp sẽ được chia đến 1mb chunk, vì vậy nếu tập tin đầu tiên là 10mb, sau đó nó sẽ trở thành 10 miếng của 1mb chunk. Và vấn đề là, mỗi đoạn sẽ dưới 1 yêu cầu http vì vậy chỉ cần tập tin đầu tiên nó sẽ được 10 yêu cầu HTTP. Hãy tưởng tượng nếu tôi có 1gb tệp, nó sẽ trở thành 1000 yêu cầu HTTP và làm chậm máy tính.
Đây là mã ví dụ:
//Prepare element progress after the page load completely
var uploaders = [];
var totalChunks = 0;
var progress;
var bars;
$(document).ready(function() {
//progress = document.querySelector('progress');
//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 file = document.getElementById('fileToUpload');
for(var i = 0; i < file.files.length; i++) {
var blob = file.files[i];
var originalFileName = blob.name;
var filePart = 0
const BYTES_PER_CHUNK = 10 * 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) {
//for Google Chrome
var chunk = blob.webkitSlice(start, end);
} else if (blob.mozSlice) {
//for Mozilla Firefox
var chunk = blob.mozSlice(start, end);
}
uploadFile(chunk, originalFileName, filePart, totalChunks, i);
filePart++;
start = end;
end = start + BYTES_PER_CHUNK;
}
}
}
function uploadFile(blobFile, fileName) {
var fd = new FormData();
fd.append("fileToUpload", blobFile);
var xm = $.ajax({
url: "upload.php"+"?"+"file1="+fileName,
type: "POST",
data: fd,
processData: false,
contentType: false,
});
}
function uploadFile(blobFile, fileName, filePart, totalChunks, divBarsSelector) {
if(filePart == 0) {
bars = document.querySelector('#bars' + divBarsSelector);
}
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 + filePart, true);
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);
}
và phần máy chủ cho nhận sẽ upload.php
$target_path = "uploads/";
$tmp_name = $_FILES['fileToUpload']['tmp_name'];
$size = $_FILES['fileToUpload']['size'];
$name = $_FILES['fileToUpload']['name'];
$originalName = $_GET['file'];
print_r("*******************************************\n");
print_r($originalName);
print_r("\n");
print_r($_FILES);
print_r("\n");
print_r("*******************************************\n");
$target_file = $target_path . basename($name);
//Result File
$complete = $originalName;
$com = fopen("uploads/".$complete, "ab");
error_log($target_path);
if ($com) {
// Read binary input stream and append it to temp file
$in = fopen($tmp_name, "rb");
if ($in) {
while ($buff = fread($in, 1048576)) {
fwrite($com, $buff);
}
}
fclose($in);
fclose($com);
}
Nếu bạn không muốn file của mình được chia thành nhiều phần, sau đó tại sao bạn chia chúng ở nơi đầu tiên? – Carsten
Không, tôi cần chúng tách ra. vì 1. PHP có giới hạn tải lên (Tôi biết tôi có thể thay đổi giới hạn đó, nhưng đó không thực sự là giải pháp thực) 2. theo cách đó tôi có thể tải lên nhiều phần cùng một lúc, điều này sẽ làm cho nó nhanh hơn. – Harts
thực sự là tập tin chia nhỏ cũng sẽ giúp tôi đạt được hồ sơ (trong trường hợp kết nối đột ngột xuống), người dùng không phải bắt đầu lại từ đầu một lần nữa – Harts