Constants const chunk size = 024 1024; // mb


Download 1.71 Kb.
Sana05.04.2023
Hajmi1.71 Kb.
#1275079
Bog'liq
chunk upload file


// Constants
const CHUNK_SIZE = 1024 * 1024; // 1 MB
const UPLOAD_URL = '/upload';

// Event listener for form submission


document.getElementById('upload-form').addEventListener('submit', function(event) {
event.preventDefault();

const file = event.target.fileInput.files[0];


const fileSize = file.size;
let chunkIndex = 0;

// Function to read and upload a chunk of the file


const uploadChunk = function(start, end) {
const xhr = new XMLHttpRequest();
const chunk = file.slice(start, end);

xhr.open('POST', UPLOAD_URL, true);

xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.setRequestHeader('X-Chunk-Index', chunkIndex);
xhr.setRequestHeader('X-File-Size', fileSize);

xhr.onload = function() {


if (xhr.status === 200) {
// Upload next chunk
chunkIndex++;
const nextStart = chunkIndex * CHUNK_SIZE;
const nextEnd = Math.min(nextStart + CHUNK_SIZE, fileSize);
if (nextStart < fileSize) {
uploadChunk(nextStart, nextEnd);
} else {
// File upload complete
console.log('Upload complete');
}
} else {
console.error('Upload failed: ' + xhr.statusText);
}
};

xhr.send(chunk);


};

// Upload first chunk


uploadChunk(0, Math.min(CHUNK_SIZE, fileSize));
});
Download 1.71 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling