From b903cca6e23a6ac6f064847c971778667dcd9140 Mon Sep 17 00:00:00 2001 From: DenisM Date: Wed, 20 May 2026 23:32:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=B0=20=D0=B1=D1=83=D0=B4=D0=B5=D1=82?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=B4...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + index.html | 29 +++++++++++++++++++++++++++++ lib_chunk_file.js | 31 +++++++++++++++++++++++++++++++ upload.php | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 .gitignore create mode 100644 index.html create mode 100644 lib_chunk_file.js create mode 100644 upload.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2333d60 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/uploads \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..e288563 --- /dev/null +++ b/index.html @@ -0,0 +1,29 @@ + + + + + + + Document + + + + + + + + + \ No newline at end of file diff --git a/lib_chunk_file.js b/lib_chunk_file.js new file mode 100644 index 0000000..25575a1 --- /dev/null +++ b/lib_chunk_file.js @@ -0,0 +1,31 @@ + +// Размер чанка в байтах +const CHUNK_SIZE = 1024 * 1024; // 1MB + +async function uploadFileInChunks(file) { + + const totalChunks = Math.ceil(file.size / CHUNK_SIZE); + + for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++) { + + // Вычисляем начало и конец текущего чанка + const start = chunkIndex * CHUNK_SIZE; + const end = Math.min(start + CHUNK_SIZE, file.size); + + // Извлекаем чанк из файла + const chunk = file.slice(start, end); + + // Создаём FormData для отправки чанка + const formData = new FormData(); + formData.append('file_chunk', chunk); + formData.append('chunk_index', chunkIndex); + formData.append('total_chunks', totalChunks); + formData.append('filename', file.name); + + // Отправляем чанк на сервер + await fetch('upload.php', { + method: 'POST', + body: formData + }); + } +} \ No newline at end of file diff --git a/upload.php b/upload.php new file mode 100644 index 0000000..db5a394 --- /dev/null +++ b/upload.php @@ -0,0 +1,38 @@ + 'error', 'message' => 'Не удалось открыть временный файл'])); + } + + // Если это последняя часть, переименовываем файл в итоговый + if ($chunkIndex === $totalChunks - 1) { + $finalFilePath = $uploadDir . $originalFilename; + rename($tempFilePath, $finalFilePath); + echo json_encode(['status' => 'success', 'message' => 'Файл успешно собран', 'path' => $finalFilePath]); + } else { + echo json_encode(['status' => 'uploading', 'chunk' => $chunkIndex]); + } +} +?> \ No newline at end of file