Тёмный
No video :(

dynamic file upload progress Vanilla JS 

`Orange.io`
Подписаться 192
Просмотров 13
50% 1

Опубликовано:

 

6 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 1   
@OfficialOrangeio
@OfficialOrangeio 2 месяца назад
/* usage: make sure to change action="your_url/post" Upload a file 0% */ const fileInput = document.getElementById('file'); const uploadBtn = document.getElementById('upload-btn'); const progressBar = document.getElementById('progress-bar'); const progressLabel = document.getElementById('progress-label'); const form = document.getElementById('upload-form'); uploadBtn.addEventListener('click', () => { const file = fileInput.files[0]; if (!file) return alert("Please select a file."); const payload = new FormData(); payload.append('user-image', file); fetchWithProgress(form.action, payload, percentComplete => { progressBar.value = percentComplete; progressLabel.textContent = `${Math.round(percentComplete)}%`; }).then(response => { if (response.ok) return response.json(); throw new Error(response.statusText); }).then(data => console.log(data)) .catch(error => console.error('Error:', error)); }); const fetchWithProgress = (url, body, onProgress) => new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('POST', url); xhr.upload.onprogress = event => { if (event.lengthComputable) onProgress((event.loaded / event.total) * 100); }; xhr.onload = () => (xhr.status >= 200 && xhr.status < 300) ? resolve({ ok: true, status: xhr.status, statusText: xhr.statusText, json: () => JSON.parse(xhr.responseText) }) : reject({ status: xhr.status, statusText: xhr.statusText }); xhr.onerror = () => reject({ status: xhr.status, statusText: xhr.statusText }); xhr.send(body); });
Далее
Brutally honest advice for new .NET Web Developers
7:19
Fake watermelon by Secret Vlog
00:16
Просмотров 3,1 млн
Should you use Ruby on Rails in 2024?
14:43
Просмотров 48 тыс.
Adobe: A Disgusting, Criminal Company
10:21
Просмотров 353 тыс.
Multi-Level Navbar Menu in Power Apps
22:50
My DREAM Everyday Tech!
18:07
Просмотров 4,4 млн
I Quit Netflix and Use These FREE Alternatives Instead
10:17
13 Things To Remove From Your Website Immediately
12:33
5 JavaScript Concepts You HAVE TO KNOW
9:38
Просмотров 1,4 млн
20 Emerging Technologies That Will Change The World
48:48