45 lines
1.5 KiB
HTML
45 lines
1.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Chunked File Upload</title>
|
|
</head>
|
|
<body>
|
|
<input type="file" id="fileInput">
|
|
<button onclick="uploadFile()">Upload</button>
|
|
|
|
<script>
|
|
async function uploadFile() {
|
|
const fileInput = document.getElementById('fileInput');
|
|
const file = fileInput.files[0];
|
|
const chunkSize = 10 * 1024 * 1024; // 10 MB
|
|
const totalChunks = Math.ceil(file.size / chunkSize);
|
|
|
|
for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++) {
|
|
const start = chunkIndex * chunkSize;
|
|
const end = Math.min(start + chunkSize, file.size);
|
|
const chunk = file.slice(start, end);
|
|
|
|
const formData = new FormData();
|
|
formData.append('chunk', chunk);
|
|
formData.append('filename', file.name);
|
|
formData.append('chunkIndex', chunkIndex);
|
|
formData.append('totalChunks', totalChunks);
|
|
|
|
const response = await fetch('/upload', {
|
|
method: 'POST',
|
|
body: formData,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
console.error('Failed to upload chunk', chunkIndex);
|
|
return;
|
|
}
|
|
}
|
|
|
|
console.log('File uploaded successfully');
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |