Video: upload progress bar with percentage (XMLHttpRequest)
This commit is contained in:
parent
b079263534
commit
7f2a6f208f
@ -771,29 +771,45 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show uploading state
|
// Show uploading state with progress
|
||||||
dropContent.innerHTML = `
|
dropContent.innerHTML = `
|
||||||
<div class="uploading-overlay">
|
<div class="uploading-overlay">
|
||||||
<div class="spinner"></div>
|
|
||||||
<div class="drop-label">Загрузка файла...</div>
|
<div class="drop-label">Загрузка файла...</div>
|
||||||
<div class="drop-hint">${file.name}</div>
|
<div style="font-size:24px;font-weight:800;color:var(--accent);font-family:'JetBrains Mono',monospace;" id="uploadPercent">0%</div>
|
||||||
|
<div class="progress-bar-track" style="width:100%;max-width:300px;">
|
||||||
|
<div class="progress-bar-fill" id="uploadFill" style="width:0%"></div>
|
||||||
|
</div>
|
||||||
|
<div class="drop-hint" style="margin-top:8px;">${file.name} (${formatSize(file.size)})</div>
|
||||||
</div>`;
|
</div>`;
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('video', file);
|
formData.append('video', file);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/video/upload', {
|
const data = await new Promise((resolve, reject) => {
|
||||||
method: 'POST',
|
const xhr = new XMLHttpRequest();
|
||||||
body: formData,
|
xhr.open('POST', '/video/upload');
|
||||||
});
|
xhr.upload.onprogress = (e) => {
|
||||||
|
if (e.lengthComputable) {
|
||||||
if (!res.ok) {
|
const pct = Math.round((e.loaded / e.total) * 100);
|
||||||
const err = await res.json().catch(() => ({ error: 'Ошибка загрузки' }));
|
const el = document.getElementById('uploadPercent');
|
||||||
throw new Error(err.error || 'Ошибка загрузки файла');
|
const fill = document.getElementById('uploadFill');
|
||||||
|
if (el) el.textContent = pct + '%';
|
||||||
|
if (fill) fill.style.width = pct + '%';
|
||||||
}
|
}
|
||||||
|
};
|
||||||
const data = await res.json();
|
xhr.onload = () => {
|
||||||
|
if (xhr.status >= 200 && xhr.status < 300) {
|
||||||
|
try { resolve(JSON.parse(xhr.responseText)); }
|
||||||
|
catch { reject(new Error('Ошибка разбора ответа')); }
|
||||||
|
} else {
|
||||||
|
try { reject(new Error(JSON.parse(xhr.responseText).error)); }
|
||||||
|
catch { reject(new Error('Ошибка загрузки')); }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.onerror = () => reject(new Error('Сеть недоступна'));
|
||||||
|
xhr.send(formData);
|
||||||
|
});
|
||||||
onUploadSuccess(data);
|
onUploadSuccess(data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Restore drop zone
|
// Restore drop zone
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user