Vanilla Javascript: Upload file

function uploadfile(file, url, success, progress){ const ajax = new XMLHttpRequest(); const formData = new FormData(); if (el instanceof Element) file=file.files[0]; formData.append('file', file); if (typeof progress=='function') { ajax.upload.addEventListener( 'progress', function ProgressHandler(event){ const p = Math.floor((event.loaded / event.total) * 100); progress(`${p}%`); } ); } if (success) ajax.addEventListener('load', success, false); ajax.open('POST', url, true); ajax.send(formData); }
Upload file using javasript/vanilla XHTTPRequest.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.