feat: added feature of http/2 chunk load
This commit is contained in:
51
wwwroot/index.html
Normal file
51
wwwroot/index.html
Normal file
@@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>HTTP2FileStreams test</title>
|
||||
</head>
|
||||
<input type="file" id="input"/>
|
||||
<button onclick="onFileSelected()">Загрузить</button>
|
||||
<script>
|
||||
function onFileSelected(){
|
||||
const inputElement = document.getElementById("input").files[0];
|
||||
uploadFile(inputElement, console.log);
|
||||
}
|
||||
async function uploadFile(file, onProgress) {
|
||||
const endpoint = '/api/upload';
|
||||
|
||||
// Создаем TransformStream для отслеживания прогресса
|
||||
let uploadedBytes = 0;
|
||||
const progressStream = new TransformStream({
|
||||
transform(chunk, controller) {
|
||||
controller.enqueue(chunk);
|
||||
uploadedBytes += chunk.length;
|
||||
const percent = Math.round((uploadedBytes / file.size) * 100);
|
||||
onProgress(percent);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
// Создаем поток для чтения файла
|
||||
const fileStream = file.stream();
|
||||
|
||||
// Соединяем потоки
|
||||
const streamWithProgress = fileStream.pipeThrough(progressStream);
|
||||
|
||||
// Отправляем запрос
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'X-File-Name': encodeURIComponent(file.name),
|
||||
},
|
||||
body: streamWithProgress,
|
||||
duplex: 'half' // Обязательно для потоковой отправки
|
||||
});
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
throw new Error(`Upload failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</html>
|
||||
Reference in New Issue
Block a user