Files
upload_big_files/lib_chunk_file.js

39 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Размер чанка в байтах
const CHUNK_SIZE = 1024 * 1024; // 1MB
async function uploadFileInChunks(file,num) {
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 для отправки чанкаd
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
}).
then((res)=> res.json()).
then((res)=>{
document.getElementById("status_bar_"+num).innerHTML = Number(res.size/1024/1024).toFixed(2) || 0
console.log(res)
});
}
}