Vanilla Javascript: Upload file
Mon Aug 29 2022 19:43:16 GMT+0000 (Coordinated Universal Time)
Saved by
@marcopinero
#javascript
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);
}
content_copyCOPY
Comments