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);
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter