diff --git a/index.html b/index.html index e288563..a64e424 100644 --- a/index.html +++ b/index.html @@ -5,10 +5,25 @@ Document + + - + diff --git a/lib_chunk_file.js b/lib_chunk_file.js index 25575a1..4e1c18c 100644 --- a/lib_chunk_file.js +++ b/lib_chunk_file.js @@ -2,9 +2,11 @@ // Размер чанка в байтах const CHUNK_SIZE = 1024 * 1024; // 1MB -async function uploadFileInChunks(file) { +async function uploadFileInChunks(file,num) { const totalChunks = Math.ceil(file.size / CHUNK_SIZE); + + for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++) { @@ -15,7 +17,7 @@ async function uploadFileInChunks(file) { // Извлекаем чанк из файла const chunk = file.slice(start, end); - // Создаём FormData для отправки чанка + // Создаём FormData для отправки чанкаd const formData = new FormData(); formData.append('file_chunk', chunk); formData.append('chunk_index', chunkIndex); @@ -26,6 +28,12 @@ async function uploadFileInChunks(file) { await fetch('upload.php', { method: 'POST', body: formData + }). + then((res)=> res.json()). + then((res)=>{ + + document.getElementById("status_bar_"+num).innerHTML = Number(res.size/1024/1024).toFixed(2) || 0 + console.log(res) }); } } \ No newline at end of file diff --git a/upload.php b/upload.php index db5a394..3a51219 100644 --- a/upload.php +++ b/upload.php @@ -22,17 +22,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { fclose($in); } fclose($out); + + + + } else { - exit(json_encode(['status' => 'error', 'message' => 'Не удалось открыть временный файл'])); + header('Content-Type: application/json; charset=utf-8'); + exit(json_encode(['status' => 'error', 'message' => 'Не удалось открыть временный файл', 'size'=>0])); } // Если это последняя часть, переименовываем файл в итоговый if ($chunkIndex === $totalChunks - 1) { $finalFilePath = $uploadDir . $originalFilename; rename($tempFilePath, $finalFilePath); - echo json_encode(['status' => 'success', 'message' => 'Файл успешно собран', 'path' => $finalFilePath]); + header('Content-Type: application/json; charset=utf-8'); + echo json_encode(['status' => 'success', 'message' => 'Файл успешно собран', 'path' => $finalFilePath, 'size'=>filesize($finalFilePath)]); } else { - echo json_encode(['status' => 'uploading', 'chunk' => $chunkIndex]); + header('Content-Type: application/json; charset=utf-8'); + echo json_encode(['status' => 'uploading', 'chunk' => $chunkIndex, 'size' => filesize($tempFilePath)]); } } ?> \ No newline at end of file